Skip to content

Commit 7704328

Browse files
committed
Auto merge of #151158 - Zalathar:rollup-okXJcXA, r=Zalathar
Rollup of 4 pull requests Successful merges: - #150846 (include `HirId`s directly in the THIR, not wrapped in `LintLevel`s) - #150979 (Avoid ICEs after bad patterns, for the other syntactic variants) - #151103 (mir_build: Simplify length-determination and indexing for array/slice patterns) - #151130 (resolve: Downgrade `ambiguous_glob_imports` to warn-by-default) r? @ghost
2 parents a6acf0f + 2567226 commit 7704328

80 files changed

Lines changed: 557 additions & 439 deletions

File tree

Some content is hidden

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

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,11 +1512,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15121512
pat_info: PatInfo<'tcx>,
15131513
) -> Ty<'tcx> {
15141514
// Type-check the path.
1515-
let _ = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
1515+
let had_err = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
15161516

15171517
// Type-check subpatterns.
15181518
match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
1519-
Ok(()) => pat_ty,
1519+
Ok(()) => match had_err {
1520+
Ok(()) => pat_ty,
1521+
Err(guar) => Ty::new_error(self.tcx, guar),
1522+
},
15201523
Err(guar) => Ty::new_error(self.tcx, guar),
15211524
}
15221525
}
@@ -1764,8 +1767,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17641767
};
17651768

17661769
// Type-check the tuple struct pattern against the expected type.
1767-
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, &pat_info.top_info);
1768-
let had_err = diag.map_err(|diag| diag.emit());
1770+
let had_err = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
17691771

17701772
// Type-check subpatterns.
17711773
if subpats.len() == variant.fields.len()
@@ -1989,11 +1991,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19891991
if let Err(reported) = self.demand_eqtype_pat(span, expected, pat_ty, &pat_info.top_info) {
19901992
// Walk subpatterns with an expected type of `err` in this case to silence
19911993
// further errors being emitted when using the bindings. #50333
1992-
let element_tys_iter = (0..max_len).map(|_| Ty::new_error(tcx, reported));
19931994
for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
19941995
self.check_pat(elem, Ty::new_error(tcx, reported), pat_info);
19951996
}
1996-
Ty::new_tup_from_iter(tcx, element_tys_iter)
1997+
Ty::new_error(tcx, reported)
19971998
} else {
19981999
for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
19992000
self.check_pat(elem, element_tys[i], pat_info);

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4465,7 +4465,7 @@ declare_lint! {
44654465
///
44664466
/// [future-incompatible]: ../index.md#future-incompatible-lints
44674467
pub AMBIGUOUS_GLOB_IMPORTS,
4468-
Deny,
4468+
Warn,
44694469
"detects certain glob imports that require reporting an ambiguity error",
44704470
@future_incompatible = FutureIncompatibleInfo {
44714471
reason: fcw!(FutureReleaseError #114095),

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,25 +1221,32 @@ pub enum ProjectionElem<V, T> {
12211221
/// thing is true of the `ConstantIndex` and `Subslice` projections below.
12221222
Index(V),
12231223

1224-
/// These indices are generated by slice patterns. Easiest to explain
1225-
/// by example:
1224+
/// These endpoint-relative indices are generated by slice/array patterns.
1225+
///
1226+
/// For array types, `offset` is always relative to the start of the array.
1227+
/// For slice types, `from_end` determines whether `offset` is relative to
1228+
/// the start or the end of the slice being inspected.
1229+
///
1230+
/// Slice-pattern indices are easiest to explain by the position of `X` in
1231+
/// these examples:
12261232
///
12271233
/// ```ignore (illustrative)
1228-
/// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
1229-
/// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
1230-
/// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
1231-
/// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
1234+
/// [X, _, .., _, _] => { offset: 0, min_length: 4, from_end: false },
1235+
/// [_, X, .., _, _] => { offset: 1, min_length: 4, from_end: false },
1236+
/// [_, _, .., X, _] => { offset: 2, min_length: 4, from_end: true },
1237+
/// [_, _, .., _, X] => { offset: 1, min_length: 4, from_end: true },
12321238
/// ```
12331239
ConstantIndex {
1234-
/// index or -index (in Python terms), depending on from_end
1240+
/// - If `from_end == false`, this is a 0-based offset from the start of the array/slice.
1241+
/// - If `from_end == true`, this is a 1-based offset from the end of the slice.
12351242
offset: u64,
12361243
/// The thing being indexed must be at least this long -- otherwise, the
12371244
/// projection is UB.
12381245
///
12391246
/// For arrays this is always the exact length.
12401247
min_length: u64,
1241-
/// Counting backwards from end? This is always false when indexing an
1242-
/// array.
1248+
/// If `true`, `offset` is a 1-based offset from the end of the slice.
1249+
/// Always false when indexing an array.
12431250
from_end: bool,
12441251
},
12451252

compiler/rustc_middle/src/thir.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ pub struct Param<'tcx> {
116116
pub hir_id: Option<HirId>,
117117
}
118118

119-
#[derive(Copy, Clone, Debug, HashStable)]
120-
pub enum LintLevel {
121-
Inherited,
122-
Explicit(HirId),
123-
}
124-
125119
#[derive(Clone, Debug, HashStable)]
126120
pub struct Block {
127121
/// Whether the block itself has a label. Used by `label: {}`
@@ -236,8 +230,8 @@ pub enum StmtKind<'tcx> {
236230
/// `let pat: ty = <INIT> else { <ELSE> }`
237231
else_block: Option<BlockId>,
238232

239-
/// The lint level for this `let` statement.
240-
lint_level: LintLevel,
233+
/// The [`HirId`] for this `let` statement.
234+
hir_id: HirId,
241235

242236
/// Span of the `let <PAT> = <INIT>` part.
243237
span: Span,
@@ -271,7 +265,7 @@ pub enum ExprKind<'tcx> {
271265
/// and to track the `HirId` of the expressions within the scope.
272266
Scope {
273267
region_scope: region::Scope,
274-
lint_level: LintLevel,
268+
hir_id: HirId,
275269
value: ExprId,
276270
},
277271
/// A `box <value>` expression.
@@ -579,7 +573,7 @@ pub struct Arm<'tcx> {
579573
pub pattern: Box<Pat<'tcx>>,
580574
pub guard: Option<ExprId>,
581575
pub body: ExprId,
582-
pub lint_level: LintLevel,
576+
pub hir_id: HirId,
583577
pub scope: region::Scope,
584578
pub span: Span,
585579
}

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
4747
use ExprKind::*;
4848
let Expr { kind, ty: _, temp_scope_id: _, span: _ } = expr;
4949
match *kind {
50-
Scope { value, region_scope: _, lint_level: _ } => {
51-
visitor.visit_expr(&visitor.thir()[value])
52-
}
50+
Scope { value, region_scope: _, hir_id: _ } => visitor.visit_expr(&visitor.thir()[value]),
5351
Box { value } => visitor.visit_expr(&visitor.thir()[value]),
5452
If { cond, then, else_opt, if_then_scope: _ } => {
5553
visitor.visit_expr(&visitor.thir()[cond]);
@@ -205,7 +203,7 @@ pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
205203
remainder_scope: _,
206204
init_scope: _,
207205
pattern,
208-
lint_level: _,
206+
hir_id: _,
209207
else_block,
210208
span: _,
211209
} => {
@@ -238,7 +236,7 @@ pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
238236
visitor: &mut V,
239237
arm: &'thir Arm<'tcx>,
240238
) {
241-
let Arm { guard, pattern, body, lint_level: _, span: _, scope: _ } = arm;
239+
let Arm { guard, pattern, body, hir_id: _, span: _, scope: _ } = arm;
242240
if let Some(expr) = guard {
243241
visitor.visit_expr(&visitor.thir()[*expr])
244242
}

compiler/rustc_mir_build/src/builder/block.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use tracing::debug;
77

88
use crate::builder::ForGuard::OutsideGuard;
99
use crate::builder::matches::{DeclareLetBindings, ScheduleDrops};
10+
use crate::builder::scope::LintLevel;
1011
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
1112

1213
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -83,7 +84,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8384
init_scope,
8485
pattern,
8586
initializer: Some(initializer),
86-
lint_level,
87+
hir_id,
8788
else_block: Some(else_block),
8889
span: _,
8990
} => {
@@ -191,7 +192,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
191192

192193
let initializer_span = this.thir[*initializer].span;
193194
let scope = (*init_scope, source_info);
194-
let failure_and_block = this.in_scope(scope, *lint_level, |this| {
195+
let lint_level = LintLevel::Explicit(*hir_id);
196+
let failure_and_block = this.in_scope(scope, lint_level, |this| {
195197
this.declare_bindings(
196198
visibility_scope,
197199
remainder_span,
@@ -232,7 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
232234
init_scope,
233235
pattern,
234236
initializer,
235-
lint_level,
237+
hir_id,
236238
else_block: None,
237239
span: _,
238240
} => {
@@ -250,12 +252,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
250252
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
251253

252254
// Evaluate the initializer, if present.
255+
let lint_level = LintLevel::Explicit(*hir_id);
253256
if let Some(init) = *initializer {
254257
let initializer_span = this.thir[init].span;
255258
let scope = (*init_scope, source_info);
256259

257260
block = this
258-
.in_scope(scope, *lint_level, |this| {
261+
.in_scope(scope, lint_level, |this| {
259262
this.declare_bindings(
260263
visibility_scope,
261264
remainder_span,
@@ -269,7 +272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
269272
.into_block();
270273
} else {
271274
let scope = (*init_scope, source_info);
272-
let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
275+
let _: BlockAnd<()> = this.in_scope(scope, lint_level, |this| {
273276
this.declare_bindings(
274277
visibility_scope,
275278
remainder_span,

compiler/rustc_mir_build/src/builder/expr/as_constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2323
let tcx = this.tcx;
2424
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
2525
match kind {
26-
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
26+
ExprKind::Scope { region_scope: _, hir_id: _, value } => {
2727
this.as_constant(&this.thir[*value])
2828
}
2929
_ => as_constant_inner(

compiler/rustc_mir_build/src/builder/expr/as_operand.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::thir::*;
66
use tracing::{debug, instrument};
77

88
use crate::builder::expr::category::Category;
9+
use crate::builder::scope::LintLevel;
910
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
1011

1112
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -122,10 +123,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
122123
let this = self; // See "LET_THIS_SELF".
123124

124125
let expr = &this.thir[expr_id];
125-
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
126+
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
126127
let source_info = this.source_info(expr.span);
127128
let region_scope = (region_scope, source_info);
128-
return this.in_scope(region_scope, lint_level, |this| {
129+
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
129130
this.as_operand(block, scope, value, local_info, needs_temporary)
130131
});
131132
}
@@ -165,10 +166,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
165166
let expr = &this.thir[expr_id];
166167
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);
167168

168-
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
169+
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
169170
let source_info = this.source_info(expr.span);
170171
let region_scope = (region_scope, source_info);
171-
return this.in_scope(region_scope, lint_level, |this| {
172+
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
172173
this.as_call_operand(block, scope, value)
173174
});
174175
}

compiler/rustc_mir_build/src/builder/expr/as_place.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tracing::{debug, instrument, trace};
1616

1717
use crate::builder::ForGuard::{OutsideGuard, RefWithinGuard};
1818
use crate::builder::expr::category::Category;
19+
use crate::builder::scope::LintLevel;
1920
use crate::builder::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};
2021

2122
/// The "outermost" place that holds this value.
@@ -427,8 +428,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
427428
let expr_span = expr.span;
428429
let source_info = this.source_info(expr_span);
429430
match expr.kind {
430-
ExprKind::Scope { region_scope, lint_level, value } => {
431-
this.in_scope((region_scope, source_info), lint_level, |this| {
431+
ExprKind::Scope { region_scope, hir_id, value } => {
432+
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
432433
this.expr_as_place(block, value, mutability, fake_borrow_temps)
433434
})
434435
}

compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tracing::debug;
1818

1919
use crate::builder::expr::as_place::PlaceBase;
2020
use crate::builder::expr::category::{Category, RvalueFunc};
21+
use crate::builder::scope::LintLevel;
2122
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
2223

2324
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -56,9 +57,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5657

5758
match expr.kind {
5859
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
59-
ExprKind::Scope { region_scope, lint_level, value } => {
60+
ExprKind::Scope { region_scope, hir_id, value } => {
6061
let region_scope = (region_scope, source_info);
61-
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
62+
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
63+
this.as_rvalue(block, scope, value)
64+
})
6265
}
6366
ExprKind::Repeat { value, count } => {
6467
if Some(0) == count.try_to_target_usize(this.tcx) {
@@ -657,7 +660,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
657660
source: eid,
658661
is_from_as_cast: _,
659662
}
660-
| &ExprKind::Scope { region_scope: _, lint_level: _, value: eid } => {
663+
| &ExprKind::Scope { region_scope: _, hir_id: _, value: eid } => {
661664
kind = &self.thir[eid].kind
662665
}
663666
_ => return matches!(Category::of(&kind), Some(Category::Constant)),

0 commit comments

Comments
 (0)