Skip to content

Commit 2c0a225

Browse files
authored
Rollup merge of #101086 - cjgillot:thir-param, r=oli-obk
Compute information about function parameters on THIR This avoids some manipulation of typeck results while building MIR.
2 parents 6395082 + b9e1806 commit 2c0a225

File tree

61 files changed

+658
-886
lines changed

Some content is hidden

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

61 files changed

+658
-886
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
360360

361361
diag.span_label(upvar_span, "captured outer variable");
362362
diag.span_label(
363-
self.body.span,
363+
self.infcx.tcx.def_span(def_id),
364364
format!("captured by this `{closure_kind}` closure"),
365365
);
366366

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
99
use rustc_middle::ty::{self, Ty, TyCtxt};
1010
use rustc_middle::{
1111
hir::place::PlaceBase,
12-
mir::{
13-
self, BindingForm, ClearCrossCrate, ImplicitSelfKind, Local, LocalDecl, LocalInfo,
14-
LocalKind, Location,
15-
},
12+
mir::{self, BindingForm, ClearCrossCrate, Local, LocalDecl, LocalInfo, LocalKind, Location},
1613
};
1714
use rustc_span::source_map::DesugaringKind;
1815
use rustc_span::symbol::{kw, Symbol};
@@ -312,7 +309,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
312309
&& !matches!(
313310
decl.local_info,
314311
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
315-
ImplicitSelfKind::MutRef
312+
hir::ImplicitSelfKind::MutRef
316313
))))
317314
)
318315
{
@@ -976,6 +973,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
976973

977974
let hir = self.infcx.tcx.hir();
978975
let closure_id = self.mir_hir_id();
976+
let closure_span = self.infcx.tcx.def_span(self.mir_def_id());
979977
let fn_call_id = hir.get_parent_node(closure_id);
980978
let node = hir.get(fn_call_id);
981979
let def_id = hir.enclosing_body_owner(fn_call_id);
@@ -1027,7 +1025,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10271025
if let Some(span) = arg {
10281026
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
10291027
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
1030-
err.span_label(self.body.span, "in this closure");
1028+
err.span_label(closure_span, "in this closure");
10311029
look_at_return = false;
10321030
}
10331031
}
@@ -1053,7 +1051,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10531051
sig.decl.output.span(),
10541052
"change this to return `FnMut` instead of `Fn`",
10551053
);
1056-
err.span_label(self.body.span, "in this closure");
1054+
err.span_label(closure_span, "in this closure");
10571055
}
10581056
_ => {}
10591057
}
@@ -1077,7 +1075,7 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
10771075
//
10781076
// Deliberately fall into this case for all implicit self types,
10791077
// so that we don't fall in to the next case with them.
1080-
*kind == mir::ImplicitSelfKind::MutRef
1078+
*kind == hir::ImplicitSelfKind::MutRef
10811079
}
10821080
_ if Some(kw::SelfLower) == local_name => {
10831081
// Otherwise, check if the name is the `self` keyword - in which case

compiler/rustc_borrowck/src/nll.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,9 @@ pub(super) fn dump_annotation<'a, 'tcx>(
389389
// viewing the intraprocedural state, the -Zdump-mir output is
390390
// better.
391391

392+
let def_span = tcx.def_span(body.source.def_id());
392393
let mut err = if let Some(closure_region_requirements) = closure_region_requirements {
393-
let mut err = tcx.sess.diagnostic().span_note_diag(body.span, "external requirements");
394+
let mut err = tcx.sess.diagnostic().span_note_diag(def_span, "external requirements");
394395

395396
regioncx.annotate(tcx, &mut err);
396397

@@ -409,7 +410,7 @@ pub(super) fn dump_annotation<'a, 'tcx>(
409410

410411
err
411412
} else {
412-
let mut err = tcx.sess.diagnostic().span_note_diag(body.span, "no external requirements");
413+
let mut err = tcx.sess.diagnostic().span_note_diag(def_span, "no external requirements");
413414
regioncx.annotate(tcx, &mut err);
414415

415416
err

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2642,7 +2642,7 @@ pub struct FnDecl<'hir> {
26422642
}
26432643

26442644
/// Represents what type of implicit self a function has, if any.
2645-
#[derive(Copy, Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2645+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
26462646
pub enum ImplicitSelfKind {
26472647
/// Represents a `fn x(self);`.
26482648
Imm,

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::captures::Captures;
1818
use rustc_errors::ErrorGuaranteed;
1919
use rustc_hir::def::{CtorKind, Namespace};
2020
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
21-
use rustc_hir::{self, GeneratorKind};
21+
use rustc_hir::{self, GeneratorKind, ImplicitSelfKind};
2222
use rustc_hir::{self as hir, HirId};
2323
use rustc_session::Session;
2424
use rustc_target::abi::{Size, VariantIdx};
@@ -653,22 +653,6 @@ pub enum BindingForm<'tcx> {
653653
RefForGuard,
654654
}
655655

656-
/// Represents what type of implicit self a function has, if any.
657-
#[derive(Clone, Copy, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
658-
pub enum ImplicitSelfKind {
659-
/// Represents a `fn x(self);`.
660-
Imm,
661-
/// Represents a `fn x(mut self);`.
662-
Mut,
663-
/// Represents a `fn x(&self);`.
664-
ImmRef,
665-
/// Represents a `fn x(&mut self);`.
666-
MutRef,
667-
/// Represents when a function does not have a self argument or
668-
/// when a function has a `self: X` argument.
669-
None,
670-
}
671-
672656
TrivialTypeTraversalAndLiftImpls! { BindingForm<'tcx>, }
673657

674658
mod binding_form_impl {

compiler/rustc_middle/src/thir.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,29 @@ macro_rules! thir_with_elements {
7373
}
7474
}
7575

76+
pub const UPVAR_ENV_PARAM: ParamId = ParamId::from_u32(0);
77+
7678
thir_with_elements! {
7779
arms: ArmId => Arm<'tcx> => "a{}",
7880
blocks: BlockId => Block => "b{}",
7981
exprs: ExprId => Expr<'tcx> => "e{}",
8082
stmts: StmtId => Stmt<'tcx> => "s{}",
83+
params: ParamId => Param<'tcx> => "p{}",
84+
}
85+
86+
/// Description of a type-checked function parameter.
87+
#[derive(Clone, Debug, HashStable)]
88+
pub struct Param<'tcx> {
89+
/// The pattern that appears in the parameter list, or None for implicit parameters.
90+
pub pat: Option<Pat<'tcx>>,
91+
/// The possibly inferred type.
92+
pub ty: Ty<'tcx>,
93+
/// Span of the explicitly provided type, or None if inferred for closures.
94+
pub ty_span: Option<Span>,
95+
/// Whether this param is `self`, and how it is bound.
96+
pub self_kind: Option<hir::ImplicitSelfKind>,
97+
/// HirId for lints.
98+
pub hir_id: Option<hir::HirId>,
8199
}
82100

83101
#[derive(Copy, Clone, Debug, HashStable)]
@@ -548,6 +566,15 @@ impl<'tcx> Pat<'tcx> {
548566
pub fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
549567
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
550568
}
569+
570+
pub fn simple_ident(&self) -> Option<Symbol> {
571+
match &*self.kind {
572+
PatKind::Binding { name, mode: BindingMode::ByValue, subpattern: None, .. } => {
573+
Some(*name)
574+
}
575+
_ => None,
576+
}
577+
}
551578
}
552579

553580
#[derive(Clone, Debug, HashStable)]

compiler/rustc_mir_build/src/build/matches/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,15 +573,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
573573

574574
_ => {
575575
let place_builder = unpack!(block = self.as_place_builder(block, initializer));
576-
self.place_into_pattern(block, irrefutable_pat, place_builder, true)
576+
self.place_into_pattern(block, &irrefutable_pat, place_builder, true)
577577
}
578578
}
579579
}
580580

581581
pub(crate) fn place_into_pattern(
582582
&mut self,
583583
block: BasicBlock,
584-
irrefutable_pat: Pat<'tcx>,
584+
irrefutable_pat: &Pat<'tcx>,
585585
initializer: PlaceBuilder<'tcx>,
586586
set_match_place: bool,
587587
) -> BlockAnd<()> {

0 commit comments

Comments
 (0)