Skip to content

Commit c84083b

Browse files
committed
Auto 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 1ce5198 + ce9daa2 commit c84083b

File tree

70 files changed

+680
-915
lines changed

Some content is hidden

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

70 files changed

+680
-915
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
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

+6-8
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
{
@@ -973,6 +970,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
973970

974971
let hir = self.infcx.tcx.hir();
975972
let closure_id = self.mir_hir_id();
973+
let closure_span = self.infcx.tcx.def_span(self.mir_def_id());
976974
let fn_call_id = hir.get_parent_node(closure_id);
977975
let node = hir.get(fn_call_id);
978976
let def_id = hir.enclosing_body_owner(fn_call_id);
@@ -1024,7 +1022,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10241022
if let Some(span) = arg {
10251023
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
10261024
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
1027-
err.span_label(self.body.span, "in this closure");
1025+
err.span_label(closure_span, "in this closure");
10281026
look_at_return = false;
10291027
}
10301028
}
@@ -1050,7 +1048,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10501048
sig.decl.output.span(),
10511049
"change this to return `FnMut` instead of `Fn`",
10521050
);
1053-
err.span_label(self.body.span, "in this closure");
1051+
err.span_label(closure_span, "in this closure");
10541052
}
10551053
_ => {}
10561054
}
@@ -1074,7 +1072,7 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
10741072
//
10751073
// Deliberately fall into this case for all implicit self types,
10761074
// so that we don't fall in to the next case with them.
1077-
*kind == mir::ImplicitSelfKind::MutRef
1075+
*kind == hir::ImplicitSelfKind::MutRef
10781076
}
10791077
_ if Some(kw::SelfLower) == local_name => {
10801078
// Otherwise, check if the name is the `self` keyword - in which case

compiler/rustc_borrowck/src/nll.rs

+3-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2661,7 +2661,7 @@ pub struct FnDecl<'hir> {
26612661
}
26622662

26632663
/// Represents what type of implicit self a function has, if any.
2664-
#[derive(Copy, Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2664+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
26652665
pub enum ImplicitSelfKind {
26662666
/// Represents a `fn x(self);`.
26672667
Imm,

compiler/rustc_middle/src/mir/mod.rs

+1-17
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

+27
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<Box<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: 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

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
568568

569569
_ => {
570570
let place_builder = unpack!(block = self.as_place_builder(block, initializer));
571-
self.place_into_pattern(block, irrefutable_pat, place_builder, true)
571+
self.place_into_pattern(block, &irrefutable_pat, place_builder, true)
572572
}
573573
}
574574
}

0 commit comments

Comments
 (0)