Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 7 additions & 6 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,11 +1512,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pat_info: PatInfo<'tcx>,
) -> Ty<'tcx> {
// Type-check the path.
let _ = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
let had_err = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);

// Type-check subpatterns.
match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
Ok(()) => pat_ty,
Ok(()) => match had_err {
Ok(()) => pat_ty,
Err(guar) => Ty::new_error(self.tcx, guar),
},
Err(guar) => Ty::new_error(self.tcx, guar),
}
}
Expand Down Expand Up @@ -1764,8 +1767,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};

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

// Type-check subpatterns.
if subpats.len() == variant.fields.len()
Expand Down Expand Up @@ -1989,11 +1991,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Err(reported) = self.demand_eqtype_pat(span, expected, pat_ty, &pat_info.top_info) {
// Walk subpatterns with an expected type of `err` in this case to silence
// further errors being emitted when using the bindings. #50333
let element_tys_iter = (0..max_len).map(|_| Ty::new_error(tcx, reported));
for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
self.check_pat(elem, Ty::new_error(tcx, reported), pat_info);
}
Ty::new_tup_from_iter(tcx, element_tys_iter)
Ty::new_error(tcx, reported)
} else {
for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
self.check_pat(elem, element_tys[i], pat_info);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4465,7 +4465,7 @@ declare_lint! {
///
/// [future-incompatible]: ../index.md#future-incompatible-lints
pub AMBIGUOUS_GLOB_IMPORTS,
Deny,
Warn,
"detects certain glob imports that require reporting an ambiguity error",
@future_incompatible = FutureIncompatibleInfo {
reason: fcw!(FutureReleaseError #114095),
Expand Down
25 changes: 16 additions & 9 deletions compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,25 +1221,32 @@ pub enum ProjectionElem<V, T> {
/// thing is true of the `ConstantIndex` and `Subslice` projections below.
Index(V),

/// These indices are generated by slice patterns. Easiest to explain
/// by example:
/// These endpoint-relative indices are generated by slice/array patterns.
///
/// For array types, `offset` is always relative to the start of the array.
/// For slice types, `from_end` determines whether `offset` is relative to
/// the start or the end of the slice being inspected.
///
/// Slice-pattern indices are easiest to explain by the position of `X` in
/// these examples:
///
/// ```ignore (illustrative)
/// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
/// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
/// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
/// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
/// [X, _, .., _, _] => { offset: 0, min_length: 4, from_end: false },
/// [_, X, .., _, _] => { offset: 1, min_length: 4, from_end: false },
/// [_, _, .., X, _] => { offset: 2, min_length: 4, from_end: true },
/// [_, _, .., _, X] => { offset: 1, min_length: 4, from_end: true },
/// ```
ConstantIndex {
/// index or -index (in Python terms), depending on from_end
/// - If `from_end == false`, this is a 0-based offset from the start of the array/slice.
/// - If `from_end == true`, this is a 1-based offset from the end of the slice.
offset: u64,
/// The thing being indexed must be at least this long -- otherwise, the
/// projection is UB.
///
/// For arrays this is always the exact length.
min_length: u64,
/// Counting backwards from end? This is always false when indexing an
/// array.
/// If `true`, `offset` is a 1-based offset from the end of the slice.
/// Always false when indexing an array.
from_end: bool,
},

Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ pub struct Param<'tcx> {
pub hir_id: Option<HirId>,
}

#[derive(Copy, Clone, Debug, HashStable)]
pub enum LintLevel {
Inherited,
Explicit(HirId),
}

#[derive(Clone, Debug, HashStable)]
pub struct Block {
/// Whether the block itself has a label. Used by `label: {}`
Expand Down Expand Up @@ -236,8 +230,8 @@ pub enum StmtKind<'tcx> {
/// `let pat: ty = <INIT> else { <ELSE> }`
else_block: Option<BlockId>,

/// The lint level for this `let` statement.
lint_level: LintLevel,
/// The [`HirId`] for this `let` statement.
hir_id: HirId,

/// Span of the `let <PAT> = <INIT>` part.
span: Span,
Expand Down Expand Up @@ -271,7 +265,7 @@ pub enum ExprKind<'tcx> {
/// and to track the `HirId` of the expressions within the scope.
Scope {
region_scope: region::Scope,
lint_level: LintLevel,
hir_id: HirId,
value: ExprId,
},
/// A `box <value>` expression.
Expand Down Expand Up @@ -579,7 +573,7 @@ pub struct Arm<'tcx> {
pub pattern: Box<Pat<'tcx>>,
pub guard: Option<ExprId>,
pub body: ExprId,
pub lint_level: LintLevel,
pub hir_id: HirId,
pub scope: region::Scope,
pub span: Span,
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
use ExprKind::*;
let Expr { kind, ty: _, temp_scope_id: _, span: _ } = expr;
match *kind {
Scope { value, region_scope: _, lint_level: _ } => {
visitor.visit_expr(&visitor.thir()[value])
}
Scope { value, region_scope: _, hir_id: _ } => visitor.visit_expr(&visitor.thir()[value]),
Box { value } => visitor.visit_expr(&visitor.thir()[value]),
If { cond, then, else_opt, if_then_scope: _ } => {
visitor.visit_expr(&visitor.thir()[cond]);
Expand Down Expand Up @@ -205,7 +203,7 @@ pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
remainder_scope: _,
init_scope: _,
pattern,
lint_level: _,
hir_id: _,
else_block,
span: _,
} => {
Expand Down Expand Up @@ -238,7 +236,7 @@ pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
arm: &'thir Arm<'tcx>,
) {
let Arm { guard, pattern, body, lint_level: _, span: _, scope: _ } = arm;
let Arm { guard, pattern, body, hir_id: _, span: _, scope: _ } = arm;
if let Some(expr) = guard {
visitor.visit_expr(&visitor.thir()[*expr])
}
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_mir_build/src/builder/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::debug;

use crate::builder::ForGuard::OutsideGuard;
use crate::builder::matches::{DeclareLetBindings, ScheduleDrops};
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -83,7 +84,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
init_scope,
pattern,
initializer: Some(initializer),
lint_level,
hir_id,
else_block: Some(else_block),
span: _,
} => {
Expand Down Expand Up @@ -191,7 +192,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let initializer_span = this.thir[*initializer].span;
let scope = (*init_scope, source_info);
let failure_and_block = this.in_scope(scope, *lint_level, |this| {
let lint_level = LintLevel::Explicit(*hir_id);
let failure_and_block = this.in_scope(scope, lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand Down Expand Up @@ -232,7 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
init_scope,
pattern,
initializer,
lint_level,
hir_id,
else_block: None,
span: _,
} => {
Expand All @@ -250,12 +252,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));

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

block = this
.in_scope(scope, *lint_level, |this| {
.in_scope(scope, lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand All @@ -269,7 +272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.into_block();
} else {
let scope = (*init_scope, source_info);
let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
let _: BlockAnd<()> = this.in_scope(scope, lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let tcx = this.tcx;
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
match kind {
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
ExprKind::Scope { region_scope: _, hir_id: _, value } => {
this.as_constant(&this.thir[*value])
}
_ => as_constant_inner(
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_mir_build/src/builder/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::thir::*;
use tracing::{debug, instrument};

use crate::builder::expr::category::Category;
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};

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

let expr = &this.thir[expr_id];
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, |this| {
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.as_operand(block, scope, value, local_info, needs_temporary)
});
}
Expand Down Expand Up @@ -165,10 +166,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr = &this.thir[expr_id];
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);

if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, |this| {
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.as_call_operand(block, scope, value)
});
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tracing::{debug, instrument, trace};

use crate::builder::ForGuard::{OutsideGuard, RefWithinGuard};
use crate::builder::expr::category::Category;
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};

/// The "outermost" place that holds this value.
Expand Down Expand Up @@ -427,8 +428,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr_span = expr.span;
let source_info = this.source_info(expr_span);
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, |this| {
ExprKind::Scope { region_scope, hir_id, value } => {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
this.expr_as_place(block, value, mutability, fake_borrow_temps)
})
}
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tracing::debug;

use crate::builder::expr::as_place::PlaceBase;
use crate::builder::expr::category::{Category, RvalueFunc};
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -56,9 +57,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

match expr.kind {
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
ExprKind::Scope { region_scope, lint_level, value } => {
ExprKind::Scope { region_scope, hir_id, value } => {
let region_scope = (region_scope, source_info);
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.as_rvalue(block, scope, value)
})
}
ExprKind::Repeat { value, count } => {
if Some(0) == count.try_to_target_usize(this.tcx) {
Expand Down Expand Up @@ -657,7 +660,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
source: eid,
is_from_as_cast: _,
}
| &ExprKind::Scope { region_scope: _, lint_level: _, value: eid } => {
| &ExprKind::Scope { region_scope: _, hir_id: _, value: eid } => {
kind = &self.thir[eid].kind
}
_ => return matches!(Category::of(&kind), Some(Category::Constant)),
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_mir_build/src/builder/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::mir::*;
use rustc_middle::thir::*;
use tracing::{debug, instrument};

use crate::builder::scope::DropKind;
use crate::builder::scope::{DropKind, LintLevel};
use crate::builder::{BlockAnd, BlockAndExtension, Builder};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -39,10 +39,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr = &this.thir[expr_id];
let expr_span = expr.span;
let source_info = this.source_info(expr_span);
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
return this.in_scope((region_scope, source_info), lint_level, |this| {
this.as_temp(block, temp_lifetime, value, mutability)
});
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
return this.in_scope(
(region_scope, source_info),
LintLevel::Explicit(hir_id),
|this| this.as_temp(block, temp_lifetime, value, mutability),
);
}

let expr_ty = expr.ty;
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tracing::{debug, instrument};

use crate::builder::expr::category::{Category, RvalueFunc};
use crate::builder::matches::{DeclareLetBindings, HasMatchGuard};
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, NeedsTemporary};
use crate::errors::{LoopMatchArmWithGuard, LoopMatchUnsupportedType};

Expand Down Expand Up @@ -45,10 +46,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

let block_and = match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
ExprKind::Scope { region_scope, hir_id, value } => {
let region_scope = (region_scope, source_info);
ensure_sufficient_stack(|| {
this.in_scope(region_scope, lint_level, |this| {
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.expr_into_dest(destination, block, value)
})
})
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_mir_build/src/builder/expr/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_middle::thir::*;
use rustc_span::source_map::Spanned;
use tracing::debug;

use crate::builder::scope::BreakableTarget;
use crate::builder::scope::{BreakableTarget, LintLevel};
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand All @@ -25,8 +25,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Handle a number of expressions that don't need a destination at all. This
// avoids needing a mountain of temporary `()` variables.
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, |this| {
ExprKind::Scope { region_scope, hir_id, value } => {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
this.stmt_expr(block, value, statement_scope)
})
}
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ExprKind::Become { value } => {
let v = &this.thir[value];
let ExprKind::Scope { value, lint_level, region_scope } = v.kind else {
let ExprKind::Scope { value, hir_id, region_scope } = v.kind else {
span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
};

Expand All @@ -115,7 +115,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
};

this.in_scope((region_scope, source_info), lint_level, |this| {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
let fun = unpack!(block = this.as_local_operand(block, fun));
let args: Box<[_]> = args
.into_iter()
Expand Down
Loading
Loading