Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 13 pull requests #121158

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0a42a54
Make `io::BorrowedCursor::advance` safe
a1phyr Feb 7, 2024
01fa720
Bump Unicode to version 15.1.0, regenerate tables
Marcondiro Feb 8, 2024
fffcb4c
Fix comment in core/src/str/validations.rs
PizzasBear Feb 12, 2024
5233bc9
Add an `ErrorGuaranteed` to `ast::TyKind::Err`.
nnethercote Feb 14, 2024
bd546fb
add extra indent spaces for rust-playground link
chenyukang Feb 14, 2024
b277772
Improve wording of static_mut_ref
obeis Feb 15, 2024
3b791a4
simplify codegen for trivially droppable types
Nov 23, 2023
fe9bc1b
reduce amount of math
Nov 23, 2023
b13c926
reduce branchiness
Nov 23, 2023
064f972
outline large copies
Nov 23, 2023
a77d6b9
add codegen test
Nov 23, 2023
da1c7f6
address review comments
Feb 11, 2024
cc7b4e0
Update aarch64 target feature docs to match LLVM
adamgemmell Jan 25, 2024
5d65b19
Avoid debug logging entire MIR body
tmiasko Feb 15, 2024
c763f83
Only point out non-diverging arms for match suggestions
compiler-errors Feb 15, 2024
acb201a
make better async fn kind errors
compiler-errors Feb 14, 2024
6018e21
Remove a suggestion that is redundant
compiler-errors Feb 15, 2024
fdc56b6
doc: add note before panicking examples for strict_overflow_ops
tspiteri Feb 15, 2024
675d092
doc: panicking division by zero examples for unsigned strict div ops
tspiteri Feb 15, 2024
954d565
Fix closure kind docs
compiler-errors Feb 15, 2024
9c78654
Rollup merge of #118264 - lukas-code:optimized-draining, r=the8472
GuillaumeGomez Feb 15, 2024
dbc1dea
Rollup merge of #120741 - a1phyr:safe_buffer_advance, r=m-ou-se
GuillaumeGomez Feb 15, 2024
128071c
Rollup merge of #120777 - Marcondiro:unicode15-1, r=Manishearth
GuillaumeGomez Feb 15, 2024
51129dc
Rollup merge of #120971 - PizzasBear:patch-1, r=Nilstrieb
GuillaumeGomez Feb 15, 2024
3f90669
Rollup merge of #121034 - obeis:improve-static-mut-ref, r=RalfJung
GuillaumeGomez Feb 15, 2024
97a7a4d
Rollup merge of #121095 - chenyukang:yukang-fix-120998-rust-playgroun…
GuillaumeGomez Feb 15, 2024
65d59fc
Rollup merge of #121109 - nnethercote:TyKind-Err-guar-2, r=oli-obk
GuillaumeGomez Feb 15, 2024
3724897
Rollup merge of #121119 - compiler-errors:async-fn-kind-errs, r=oli-obk
GuillaumeGomez Feb 15, 2024
63b058d
Rollup merge of #121141 - compiler-errors:closure-kind-docs, r=nnethe…
GuillaumeGomez Feb 15, 2024
94df9c9
Rollup merge of #121145 - adamgemmell:dev/adagem01/combined-target-fe…
GuillaumeGomez Feb 15, 2024
bf7005b
Rollup merge of #121146 - compiler-errors:ignore-diverging-arms, r=es…
GuillaumeGomez Feb 15, 2024
cafa1ae
Rollup merge of #121147 - tmiasko:no-debug-body, r=compiler-errors
GuillaumeGomez Feb 15, 2024
35f8b3d
Rollup merge of #121155 - tspiteri:strict-doc-overflow, r=Nilstrieb
GuillaumeGomez Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2136,10 +2136,12 @@ pub enum TyKind {
ImplicitSelf,
/// A macro in the type position.
MacCall(P<MacCall>),
/// Placeholder for a kind that has failed to be defined.
Err,
/// Placeholder for a `va_list`.
CVarArgs,
/// Sometimes we need a dummy value when no error has occurred.
Dummy,
/// Placeholder for a kind that has failed to be defined.
Err(ErrorGuaranteed),
}

impl TyKind {
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,12 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
let Ty { id, kind, span, tokens } = ty.deref_mut();
vis.visit_id(id);
match kind {
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}
TyKind::Infer
| TyKind::ImplicitSelf
| TyKind::Err(_)
| TyKind::Dummy
| TyKind::Never
| TyKind::CVarArgs => {}
TyKind::Slice(ty) => vis.visit_ty(ty),
TyKind::Ptr(mt) => vis.visit_mt(mt),
TyKind::Ref(lt, mt) => {
Expand Down Expand Up @@ -1649,7 +1654,7 @@ impl DummyAstNode for Ty {
fn dummy() -> Self {
Ty {
id: DUMMY_NODE_ID,
kind: TyKind::Err,
kind: TyKind::Dummy,
span: Default::default(),
tokens: Default::default(),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
}
TyKind::Typeof(expression) => visitor.visit_anon_const(expression),
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Dummy | TyKind::Err(_) => {}
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
TyKind::Never | TyKind::CVarArgs => {}
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
let kind = match &t.kind {
TyKind::Infer => hir::TyKind::Infer,
TyKind::Err => hir::TyKind::Err(self.dcx().has_errors().unwrap()),
TyKind::Err(guar) => hir::TyKind::Err(*guar),
// Lower the anonymous structs or unions in a nested lowering context.
//
// ```
Expand Down Expand Up @@ -1504,6 +1504,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
);
hir::TyKind::Err(guar)
}
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
};

hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
&item.vis,
errors::VisibilityNotPermittedNote::TraitImpl,
);
if let TyKind::Err = self_ty.kind {
// njn: use Dummy here
if let TyKind::Err(_) = self_ty.kind {
this.dcx().emit_err(errors::ObsoleteAuto { span: item.span });
}
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,11 +1048,16 @@ impl<'a> State<'a> {
ast::TyKind::Infer => {
self.word("_");
}
ast::TyKind::Err => {
ast::TyKind::Err(_) => {
self.popen();
self.word("/*ERROR*/");
self.pclose();
}
ast::TyKind::Dummy => {
self.popen();
self.word("/*DUMMY*/");
self.pclose();
}
ast::TyKind::ImplicitSelf => {
self.word("Self");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ fn start<T: Termination + 'static>(

static mut NUM: u8 = 6 * 7;

// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
#[allow(static_mut_ref)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
static NUM_REF: &'static u8 = unsafe { &NUM };

unsafe fn zeroed<T>() -> T {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ fn start<T: Termination + 'static>(

static mut NUM: u8 = 6 * 7;

// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
#[allow(static_mut_ref)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
static NUM_REF: &'static u8 = unsafe { &NUM };

macro_rules! assert {
Expand Down
26 changes: 15 additions & 11 deletions compiler/rustc_error_codes/src/error_codes/E0796.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
Reference of mutable static.
You have created a reference to a mutable static.

Erroneous code example:

```compile_fail,edition2024,E0796
static mut X: i32 = 23;
static mut Y: i32 = 24;

unsafe {
let y = &X;
let ref x = X;
let (x, y) = (&X, &Y);
foo(&X);
fn work() {
let _val = unsafe { X };
}

fn foo<'a>(_x: &'a i32) {}
let x_ref = unsafe { &mut X };
work();
// The next line has Undefined Behavior!
// `x_ref` is a mutable reference and allows no aliases,
// but `work` has been reading the reference between
// the moment `x_ref` was created and when it was used.
// This violates the uniqueness of `x_ref`.
*x_ref = 42;
```

Mutable statics can be written to by multiple threads: aliasing violations or
data races will cause undefined behavior.
A reference to a mutable static has lifetime `'static`. This is very dangerous
as it is easy to accidentally overlap the lifetime of that reference with
other, conflicting accesses to the same static.

Reference of mutable static is a hard error from 2024 edition.
References to mutable statics are a hard error in the 2024 edition.
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,13 @@ impl DummyResult {
}

/// A plain dummy type.
pub fn raw_ty(sp: Span, is_error: bool) -> P<ast::Ty> {
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
// FIXME(nnethercote): you might expect `ast::TyKind::Dummy` to be used here, but some
// values produced here end up being lowered to HIR, which `ast::TyKind::Dummy` does not
// support, so we use an empty tuple instead.
P(ast::Ty {
id: ast::DUMMY_NODE_ID,
kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(ThinVec::new()) },
kind: ast::TyKind::Tup(ThinVec::new()),
span: sp,
tokens: None,
})
Expand Down Expand Up @@ -611,7 +614,7 @@ impl MacResult for DummyResult {
}

fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
Some(DummyResult::raw_ty(self.span, self.is_error))
Some(DummyResult::raw_ty(self.span))
}

fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {
Expand Down
31 changes: 18 additions & 13 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,24 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
.label = `#[start]` function is not allowed to be `#[track_caller]`

hir_analysis_static_mut_ref = reference of mutable static is disallowed
.label = reference of mutable static
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer

hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
.label = shared reference of mutable static
.label_mut = mutable reference of mutable static
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
.note = reference of mutable static is a hard error from 2024 edition
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
hir_analysis_static_mut_ref = creating a {$shared} reference to a mutable static
.label = {$shared}reference to mutable static
.note = {$shared ->
[shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
*[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
}
.suggestion = use `addr_of!` instead to create a raw pointer
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer

hir_analysis_static_mut_refs_lint = creating a {$shared} reference to mutable static is discouraged
.label = {$shared} reference to mutable static
.suggestion = use `addr_of!` instead to create a raw pointer
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
.note = this will be a hard error in the 2024 edition
.why_note = {$shared ->
[shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
*[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
}

hir_analysis_static_specialize = cannot specialize on `'static` lifetime

Expand Down
28 changes: 10 additions & 18 deletions compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hir as hir;
use rustc_hir_pretty::qpath_to_string;
use rustc_lint_defs::builtin::STATIC_MUT_REF;
use rustc_lint_defs::builtin::STATIC_MUT_REFS;
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
use rustc_type_ir::Mutability;
Expand Down Expand Up @@ -66,32 +66,24 @@ fn handle_static_mut_ref(
hir_id: hir::HirId,
) {
if e2024 {
let sugg = if mutable {
errors::StaticMutRefSugg::Mut { span, var }
let (sugg, shared) = if mutable {
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
} else {
errors::StaticMutRefSugg::Shared { span, var }
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
};
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg, shared });
return;
}

let (label, sugg, shared) = if mutable {
(
errors::RefOfMutStaticLabel::Mut { span },
errors::RefOfMutStaticSugg::Mut { span, var },
"mutable ",
)
let (sugg, shared) = if mutable {
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
} else {
(
errors::RefOfMutStaticLabel::Shared { span },
errors::RefOfMutStaticSugg::Shared { span, var },
"shared ",
)
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
};
tcx.emit_node_span_lint(
STATIC_MUT_REF,
STATIC_MUT_REFS,
hir_id,
span,
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
errors::RefOfMutStatic { span, sugg, shared },
);
}
28 changes: 7 additions & 21 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,12 +1455,13 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> {
#[derive(Diagnostic)]
#[diag(hir_analysis_static_mut_ref, code = E0796)]
#[note]
pub struct StaticMutRef {
pub struct StaticMutRef<'a> {
#[primary_span]
#[label]
pub span: Span,
#[subdiagnostic]
pub sugg: StaticMutRefSugg,
pub shared: &'a str,
}

#[derive(Subdiagnostic)]
Expand Down Expand Up @@ -1491,30 +1492,15 @@ pub enum StaticMutRefSugg {

// STATIC_MUT_REF lint
#[derive(LintDiagnostic)]
#[diag(hir_analysis_static_mut_ref_lint)]
#[diag(hir_analysis_static_mut_refs_lint)]
#[note]
#[note(hir_analysis_why_note)]
pub struct RefOfMutStatic<'a> {
pub shared: &'a str,
#[note(hir_analysis_why_note)]
pub why_note: (),
#[subdiagnostic]
pub label: RefOfMutStaticLabel,
#[label]
pub span: Span,
#[subdiagnostic]
pub sugg: RefOfMutStaticSugg,
}

#[derive(Subdiagnostic)]
pub enum RefOfMutStaticLabel {
#[label(hir_analysis_label)]
Shared {
#[primary_span]
span: Span,
},
#[label(hir_analysis_label_mut)]
Mut {
#[primary_span]
span: Span,
},
pub shared: &'a str,
}

#[derive(Subdiagnostic)]
Expand Down
15 changes: 7 additions & 8 deletions compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
CoerceMany::with_coercion_sites(coerce_first, arms)
};

let mut other_arms = vec![]; // Used only for diagnostics.
let mut prior_non_diverging_arms = vec![]; // Used only for diagnostics.
let mut prior_arm = None;
for arm in arms {
if let Some(e) = &arm.guard {
Expand Down Expand Up @@ -118,9 +118,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
prior_arm_ty,
prior_arm_span,
scrut_span: scrut.span,
scrut_hir_id: scrut.hir_id,
source: match_src,
prior_arms: other_arms.clone(),
prior_non_diverging_arms: prior_non_diverging_arms.clone(),
opt_suggest_box_span,
})),
),
Expand All @@ -142,16 +141,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
false,
);

other_arms.push(arm_span);
if other_arms.len() > 5 {
other_arms.remove(0);
}

if !arm_ty.is_never() {
// When a match arm has type `!`, then it doesn't influence the expected type for
// the following arm. If all of the prior arms are `!`, then the influence comes
// from elsewhere and we shouldn't point to any previous arm.
prior_arm = Some((arm_block_id, arm_ty, arm_span));

prior_non_diverging_arms.push(arm_span);
if prior_non_diverging_arms.len() > 5 {
prior_non_diverging_arms.remove(0);
}
}
}

Expand Down
Loading
Loading