Skip to content

Commit 204a1dd

Browse files
committed
Auto merge of rust-lang#121158 - GuillaumeGomez:rollup-l38pzjw, r=GuillaumeGomez
Rollup of 13 pull requests Successful merges: - rust-lang#118264 (Optimize `VecDeque::drain` for (half-)open ranges) - rust-lang#120741 (Make `io::BorrowedCursor::advance` safe) - rust-lang#120777 (Bump Unicode to version 15.1.0, regenerate tables) - rust-lang#120971 (Fix comment in core/src/str/validations.rs) - rust-lang#121034 (Improve wording of `static_mut_ref`) - rust-lang#121095 (Add extra indent spaces for rust-playground link) - rust-lang#121109 (Add an ErrorGuaranteed to ast::TyKind::Err (attempt 2)) - rust-lang#121119 (Make `async Fn` trait kind errors better) - rust-lang#121141 (Fix closure kind docs) - rust-lang#121145 (Update aarch64 target feature docs to match LLVM) - rust-lang#121146 (Only point out non-diverging arms for match suggestions) - rust-lang#121147 (Avoid debug logging entire MIR body) - rust-lang#121155 (doc: add note about panicking examples for strict_overflow_ops) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b656f51 + 35f8b3d commit 204a1dd

File tree

130 files changed

+1288
-825
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1288
-825
lines changed

compiler/rustc_ast/src/ast.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2136,10 +2136,12 @@ pub enum TyKind {
21362136
ImplicitSelf,
21372137
/// A macro in the type position.
21382138
MacCall(P<MacCall>),
2139-
/// Placeholder for a kind that has failed to be defined.
2140-
Err,
21412139
/// Placeholder for a `va_list`.
21422140
CVarArgs,
2141+
/// Sometimes we need a dummy value when no error has occurred.
2142+
Dummy,
2143+
/// Placeholder for a kind that has failed to be defined.
2144+
Err(ErrorGuaranteed),
21432145
}
21442146

21452147
impl TyKind {

compiler/rustc_ast/src/mut_visit.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,12 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
481481
let Ty { id, kind, span, tokens } = ty.deref_mut();
482482
vis.visit_id(id);
483483
match kind {
484-
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}
484+
TyKind::Infer
485+
| TyKind::ImplicitSelf
486+
| TyKind::Err(_)
487+
| TyKind::Dummy
488+
| TyKind::Never
489+
| TyKind::CVarArgs => {}
485490
TyKind::Slice(ty) => vis.visit_ty(ty),
486491
TyKind::Ptr(mt) => vis.visit_mt(mt),
487492
TyKind::Ref(lt, mt) => {
@@ -1649,7 +1654,7 @@ impl DummyAstNode for Ty {
16491654
fn dummy() -> Self {
16501655
Ty {
16511656
id: DUMMY_NODE_ID,
1652-
kind: TyKind::Err,
1657+
kind: TyKind::Dummy,
16531658
span: Default::default(),
16541659
tokens: Default::default(),
16551660
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
447447
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
448448
}
449449
TyKind::Typeof(expression) => visitor.visit_anon_const(expression),
450-
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
450+
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Dummy | TyKind::Err(_) => {}
451451
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
452452
TyKind::Never | TyKind::CVarArgs => {}
453453
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {

compiler/rustc_ast_lowering/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12861286
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
12871287
let kind = match &t.kind {
12881288
TyKind::Infer => hir::TyKind::Infer,
1289-
TyKind::Err => hir::TyKind::Err(self.dcx().has_errors().unwrap()),
1289+
TyKind::Err(guar) => hir::TyKind::Err(*guar),
12901290
// Lower the anonymous structs or unions in a nested lowering context.
12911291
//
12921292
// ```
@@ -1504,6 +1504,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15041504
);
15051505
hir::TyKind::Err(guar)
15061506
}
1507+
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
15071508
};
15081509

15091510
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }

compiler/rustc_ast_passes/src/ast_validation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
881881
&item.vis,
882882
errors::VisibilityNotPermittedNote::TraitImpl,
883883
);
884-
if let TyKind::Err = self_ty.kind {
884+
// njn: use Dummy here
885+
if let TyKind::Err(_) = self_ty.kind {
885886
this.dcx().emit_err(errors::ObsoleteAuto { span: item.span });
886887
}
887888
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)

compiler/rustc_ast_pretty/src/pprust/state.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1048,11 +1048,16 @@ impl<'a> State<'a> {
10481048
ast::TyKind::Infer => {
10491049
self.word("_");
10501050
}
1051-
ast::TyKind::Err => {
1051+
ast::TyKind::Err(_) => {
10521052
self.popen();
10531053
self.word("/*ERROR*/");
10541054
self.pclose();
10551055
}
1056+
ast::TyKind::Dummy => {
1057+
self.popen();
1058+
self.word("/*DUMMY*/");
1059+
self.pclose();
1060+
}
10561061
ast::TyKind::ImplicitSelf => {
10571062
self.word("Self");
10581063
}

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ fn start<T: Termination + 'static>(
112112

113113
static mut NUM: u8 = 6 * 7;
114114

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

119119
unsafe fn zeroed<T>() -> T {

compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ fn start<T: Termination + 'static>(
9999

100100
static mut NUM: u8 = 6 * 7;
101101

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

106106
macro_rules! assert {
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
Reference of mutable static.
1+
You have created a reference to a mutable static.
22

33
Erroneous code example:
44

55
```compile_fail,edition2024,E0796
66
static mut X: i32 = 23;
7-
static mut Y: i32 = 24;
87
9-
unsafe {
10-
let y = &X;
11-
let ref x = X;
12-
let (x, y) = (&X, &Y);
13-
foo(&X);
8+
fn work() {
9+
let _val = unsafe { X };
1410
}
1511
16-
fn foo<'a>(_x: &'a i32) {}
12+
let x_ref = unsafe { &mut X };
13+
work();
14+
// The next line has Undefined Behavior!
15+
// `x_ref` is a mutable reference and allows no aliases,
16+
// but `work` has been reading the reference between
17+
// the moment `x_ref` was created and when it was used.
18+
// This violates the uniqueness of `x_ref`.
19+
*x_ref = 42;
1720
```
1821

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

22-
Reference of mutable static is a hard error from 2024 edition.
26+
References to mutable statics are a hard error in the 2024 edition.

compiler/rustc_expand/src/base.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,13 @@ impl DummyResult {
567567
}
568568

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

613616
fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
614-
Some(DummyResult::raw_ty(self.span, self.is_error))
617+
Some(DummyResult::raw_ty(self.span))
615618
}
616619

617620
fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {

compiler/rustc_hir_analysis/messages.ftl

+18-13
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,24 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
373373
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
374374
.label = `#[start]` function is not allowed to be `#[track_caller]`
375375
376-
hir_analysis_static_mut_ref = reference of mutable static is disallowed
377-
.label = reference of mutable static
378-
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
379-
.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
380-
.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
381-
382-
hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
383-
.label = shared reference of mutable static
384-
.label_mut = mutable reference of mutable static
385-
.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
386-
.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
387-
.note = reference of mutable static is a hard error from 2024 edition
388-
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
376+
hir_analysis_static_mut_ref = creating a {$shared} reference to a mutable static
377+
.label = {$shared}reference to mutable static
378+
.note = {$shared ->
379+
[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
380+
*[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
381+
}
382+
.suggestion = use `addr_of!` instead to create a raw pointer
383+
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
384+
385+
hir_analysis_static_mut_refs_lint = creating a {$shared} reference to mutable static is discouraged
386+
.label = {$shared} reference to mutable static
387+
.suggestion = use `addr_of!` instead to create a raw pointer
388+
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
389+
.note = this will be a hard error in the 2024 edition
390+
.why_note = {$shared ->
391+
[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
392+
*[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
393+
}
389394
390395
hir_analysis_static_specialize = cannot specialize on `'static` lifetime
391396

compiler/rustc_hir_analysis/src/check/errs.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir as hir;
22
use rustc_hir_pretty::qpath_to_string;
3-
use rustc_lint_defs::builtin::STATIC_MUT_REF;
3+
use rustc_lint_defs::builtin::STATIC_MUT_REFS;
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::Span;
66
use rustc_type_ir::Mutability;
@@ -66,32 +66,24 @@ fn handle_static_mut_ref(
6666
hir_id: hir::HirId,
6767
) {
6868
if e2024 {
69-
let sugg = if mutable {
70-
errors::StaticMutRefSugg::Mut { span, var }
69+
let (sugg, shared) = if mutable {
70+
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
7171
} else {
72-
errors::StaticMutRefSugg::Shared { span, var }
72+
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
7373
};
74-
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
74+
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg, shared });
7575
return;
7676
}
7777

78-
let (label, sugg, shared) = if mutable {
79-
(
80-
errors::RefOfMutStaticLabel::Mut { span },
81-
errors::RefOfMutStaticSugg::Mut { span, var },
82-
"mutable ",
83-
)
78+
let (sugg, shared) = if mutable {
79+
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
8480
} else {
85-
(
86-
errors::RefOfMutStaticLabel::Shared { span },
87-
errors::RefOfMutStaticSugg::Shared { span, var },
88-
"shared ",
89-
)
81+
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
9082
};
9183
tcx.emit_node_span_lint(
92-
STATIC_MUT_REF,
84+
STATIC_MUT_REFS,
9385
hir_id,
9486
span,
95-
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
87+
errors::RefOfMutStatic { span, sugg, shared },
9688
);
9789
}

compiler/rustc_hir_analysis/src/errors.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -1455,12 +1455,13 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> {
14551455
#[derive(Diagnostic)]
14561456
#[diag(hir_analysis_static_mut_ref, code = E0796)]
14571457
#[note]
1458-
pub struct StaticMutRef {
1458+
pub struct StaticMutRef<'a> {
14591459
#[primary_span]
14601460
#[label]
14611461
pub span: Span,
14621462
#[subdiagnostic]
14631463
pub sugg: StaticMutRefSugg,
1464+
pub shared: &'a str,
14641465
}
14651466

14661467
#[derive(Subdiagnostic)]
@@ -1491,30 +1492,15 @@ pub enum StaticMutRefSugg {
14911492

14921493
// STATIC_MUT_REF lint
14931494
#[derive(LintDiagnostic)]
1494-
#[diag(hir_analysis_static_mut_ref_lint)]
1495+
#[diag(hir_analysis_static_mut_refs_lint)]
14951496
#[note]
1497+
#[note(hir_analysis_why_note)]
14961498
pub struct RefOfMutStatic<'a> {
1497-
pub shared: &'a str,
1498-
#[note(hir_analysis_why_note)]
1499-
pub why_note: (),
1500-
#[subdiagnostic]
1501-
pub label: RefOfMutStaticLabel,
1499+
#[label]
1500+
pub span: Span,
15021501
#[subdiagnostic]
15031502
pub sugg: RefOfMutStaticSugg,
1504-
}
1505-
1506-
#[derive(Subdiagnostic)]
1507-
pub enum RefOfMutStaticLabel {
1508-
#[label(hir_analysis_label)]
1509-
Shared {
1510-
#[primary_span]
1511-
span: Span,
1512-
},
1513-
#[label(hir_analysis_label_mut)]
1514-
Mut {
1515-
#[primary_span]
1516-
span: Span,
1517-
},
1503+
pub shared: &'a str,
15181504
}
15191505

15201506
#[derive(Subdiagnostic)]

compiler/rustc_hir_typeck/src/_match.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979
CoerceMany::with_coercion_sites(coerce_first, arms)
8080
};
8181

82-
let mut other_arms = vec![]; // Used only for diagnostics.
82+
let mut prior_non_diverging_arms = vec![]; // Used only for diagnostics.
8383
let mut prior_arm = None;
8484
for arm in arms {
8585
if let Some(e) = &arm.guard {
@@ -118,9 +118,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
118118
prior_arm_ty,
119119
prior_arm_span,
120120
scrut_span: scrut.span,
121-
scrut_hir_id: scrut.hir_id,
122121
source: match_src,
123-
prior_arms: other_arms.clone(),
122+
prior_non_diverging_arms: prior_non_diverging_arms.clone(),
124123
opt_suggest_box_span,
125124
})),
126125
),
@@ -142,16 +141,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
142141
false,
143142
);
144143

145-
other_arms.push(arm_span);
146-
if other_arms.len() > 5 {
147-
other_arms.remove(0);
148-
}
149-
150144
if !arm_ty.is_never() {
151145
// When a match arm has type `!`, then it doesn't influence the expected type for
152146
// the following arm. If all of the prior arms are `!`, then the influence comes
153147
// from elsewhere and we shouldn't point to any previous arm.
154148
prior_arm = Some((arm_block_id, arm_ty, arm_span));
149+
150+
prior_non_diverging_arms.push(arm_span);
151+
if prior_non_diverging_arms.len() > 5 {
152+
prior_non_diverging_arms.remove(0);
153+
}
155154
}
156155
}
157156

0 commit comments

Comments
 (0)