Skip to content

Commit 5d42316

Browse files
committed
Extend HIR to track the source of a type
1 parent e1d5019 commit 5d42316

File tree

9 files changed

+73
-24
lines changed

9 files changed

+73
-24
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use hir::{BodyId, HirId};
4444
use rustc_abi::ExternAbi;
4545
use rustc_ast::*;
4646
use rustc_errors::ErrorGuaranteed;
47+
use rustc_hir::TySource;
4748
use rustc_hir::def_id::DefId;
4849
use rustc_middle::span_bug;
4950
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
@@ -174,12 +175,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
174175
hir_id: self.next_id(),
175176
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Input(arg)),
176177
span,
178+
source: TySource::Other,
177179
}));
178180

179181
let output = self.arena.alloc(hir::Ty {
180182
hir_id: self.next_id(),
181183
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Output),
182184
span,
185+
source: TySource::Other,
183186
});
184187

185188
self.arena.alloc(hir::FnDecl {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use rustc_ast::ptr::P as AstP;
55
use rustc_ast::*;
66
use rustc_ast_pretty::pprust::expr_to_string;
77
use rustc_data_structures::stack::ensure_sufficient_stack;
8-
use rustc_hir as hir;
9-
use rustc_hir::HirId;
108
use rustc_hir::def::{DefKind, Res};
9+
use rustc_hir::{self as hir, HirId, TySource};
1110
use rustc_middle::span_bug;
1211
use rustc_middle::ty::TyCtxt;
1312
use rustc_session::errors::report_lit_error;
@@ -761,6 +760,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
761760
hir_id: self.next_id(),
762761
kind: hir::TyKind::Path(resume_ty),
763762
span: unstable_span,
763+
source: TySource::Other,
764764
};
765765
let inputs = arena_vec![self; input_ty];
766766

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5555
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5656
use rustc_hir::{
5757
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem,
58-
LifetimeSource, LifetimeSyntax, ParamName, TraitCandidate,
58+
LifetimeSource, LifetimeSyntax, ParamName, TraitCandidate, TySource,
5959
};
6060
use rustc_index::{Idx, IndexSlice, IndexVec};
6161
use rustc_macros::extension;
@@ -1181,7 +1181,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11811181
bounds,
11821182
TaggedRef::new(lifetime_bound, TraitObjectSyntax::None),
11831183
);
1184-
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1184+
return hir::Ty {
1185+
kind,
1186+
span: self.lower_span(t.span),
1187+
hir_id: self.next_id(),
1188+
source: TySource::Other,
1189+
};
11851190
}
11861191

11871192
let id = self.lower_node_id(t.id);
@@ -1198,7 +1203,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11981203
}
11991204

12001205
fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1201-
hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
1206+
hir::Ty {
1207+
hir_id: self.next_id(),
1208+
kind,
1209+
span: self.lower_span(span),
1210+
source: TySource::Other,
1211+
}
12021212
}
12031213

12041214
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
@@ -1219,7 +1229,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12191229
let lifetime = self.lower_ty_direct_lifetime(t, *region);
12201230
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));
12211231
let span = self.lower_span(t.span);
1222-
let arg = hir::Ty { kind, span, hir_id: self.next_id() };
1232+
let arg = hir::Ty { kind, span, hir_id: self.next_id(), source: TySource::Other };
12231233
let args = self.arena.alloc(hir::GenericArgs {
12241234
args: self.arena.alloc([hir::GenericArg::Type(self.arena.alloc(arg))]),
12251235
constraints: &[],
@@ -1384,7 +1394,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13841394
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
13851395
};
13861396

1387-
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1397+
hir::Ty {
1398+
kind,
1399+
span: self.lower_span(t.span),
1400+
hir_id: self.lower_node_id(t.id),
1401+
source: TySource::Other,
1402+
}
13881403
}
13891404

13901405
fn lower_ty_direct_lifetime(
@@ -2382,7 +2397,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23822397
}
23832398
}
23842399

2385-
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2400+
fn ty_path(&mut self, hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2401+
self.ty_path_with_source(hir_id, span, qpath, TySource::Other)
2402+
}
2403+
2404+
fn ty_path_with_source(
2405+
&mut self,
2406+
mut hir_id: HirId,
2407+
span: Span,
2408+
qpath: hir::QPath<'hir>,
2409+
source: TySource,
2410+
) -> hir::Ty<'hir> {
23862411
let kind = match qpath {
23872412
hir::QPath::Resolved(None, path) => {
23882413
// Turn trait object paths into `TyKind::TraitObject` instead.
@@ -2409,7 +2434,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24092434
_ => hir::TyKind::Path(qpath),
24102435
};
24112436

2412-
hir::Ty { hir_id, kind, span: self.lower_span(span) }
2437+
hir::Ty { hir_id, kind, span: self.lower_span(span), source }
24132438
}
24142439

24152440
/// Invoked to create the lifetime argument(s) for an elided trait object

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use rustc_ast::{self as ast, *};
44
use rustc_hir::def::{DefKind, PartialRes, PerNS, Res};
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::{self as hir, GenericArg};
6+
use rustc_hir::{self as hir, GenericArg, TySource};
77
use rustc_middle::{span_bug, ty};
88
use rustc_session::parse::add_feature_diagnostics;
99
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
@@ -169,7 +169,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
169169
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
170170
// `<I as Iterator>::Item::default`.
171171
let new_id = self.next_id();
172-
self.arena.alloc(self.ty_path(new_id, path.span, hir::QPath::Resolved(qself, path)))
172+
self.arena.alloc(self.ty_path_with_source(
173+
new_id,
174+
path.span,
175+
hir::QPath::Resolved(qself, path),
176+
TySource::ImplicitSelf,
177+
))
173178
};
174179

175180
// Anything after the base path are associated "extensions",

compiler/rustc_hir/src/hir.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,12 @@ pub struct InferArg {
498498

499499
impl InferArg {
500500
pub fn to_ty(&self) -> Ty<'static> {
501-
Ty { kind: TyKind::Infer(()), span: self.span, hir_id: self.hir_id }
501+
Ty {
502+
kind: TyKind::Infer(()),
503+
span: self.span,
504+
hir_id: self.hir_id,
505+
source: TySource::Other,
506+
}
502507
}
503508
}
504509

@@ -3274,6 +3279,16 @@ impl<'hir> AssocItemConstraintKind<'hir> {
32743279
}
32753280
}
32763281

3282+
#[derive(Debug, Clone, Copy, PartialEq, HashStable_Generic)]
3283+
pub enum TySource {
3284+
/// `Vec` in `Vec::new`
3285+
ImplicitSelf,
3286+
3287+
/// Details not yet needed. Feel free to give useful
3288+
/// categorization to these usages.
3289+
Other,
3290+
}
3291+
32773292
/// An uninhabited enum used to make `Infer` variants on [`Ty`] and [`ConstArg`] be
32783293
/// unreachable. Zero-Variant enums are guaranteed to have the same layout as the never
32793294
/// type.
@@ -3293,6 +3308,7 @@ pub struct Ty<'hir, Unambig = ()> {
32933308
pub hir_id: HirId,
32943309
pub span: Span,
32953310
pub kind: TyKind<'hir, Unambig>,
3311+
pub source: TySource,
32963312
}
32973313

32983314
impl<'hir> Ty<'hir, AmbigArg> {
@@ -4991,7 +5007,7 @@ mod size_asserts {
49915007
static_assert_size!(StmtKind<'_>, 16);
49925008
static_assert_size!(TraitItem<'_>, 88);
49935009
static_assert_size!(TraitItemKind<'_>, 48);
4994-
static_assert_size!(Ty<'_>, 48);
5010+
static_assert_size!(Ty<'_>, 56);
49955011
static_assert_size!(TyKind<'_>, 32);
49965012
// tidy-alphabetical-end
49975013
}

compiler/rustc_hir/src/hir/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ macro_rules! define_tests {
2020

2121
define_tests! {
2222
cast_never TyKind Never {}
23-
cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }] }
24-
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }, mutbl: Mutability::Not }}
23+
cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never, source: TySource::Other }] }
24+
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never, source: TySource::Other }, mutbl: Mutability::Not }}
2525
cast_array TyKind Array {
26-
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never },
26+
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never, source: TySource::Other },
2727
1: &ConstArg { hir_id: HirId::INVALID, kind: ConstArgKind::Anon(&AnonConst {
2828
hir_id: HirId::INVALID,
2929
def_id: LocalDefId { local_def_index: DefIndex::ZERO },

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,15 +978,15 @@ pub fn walk_unambig_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) ->
978978
match typ.try_as_ambig_ty() {
979979
Some(ambig_ty) => visitor.visit_ty(ambig_ty),
980980
None => {
981-
let Ty { hir_id, span, kind: _ } = typ;
981+
let Ty { hir_id, span, kind: _, source: _ } = typ;
982982
try_visit!(visitor.visit_id(*hir_id));
983983
visitor.visit_infer(*hir_id, *span, InferKind::Ty(typ))
984984
}
985985
}
986986
}
987987

988988
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v, AmbigArg>) -> V::Result {
989-
let Ty { hir_id, span: _, kind } = typ;
989+
let Ty { hir_id, span: _, kind, source: _ } = typ;
990990
try_visit!(visitor.visit_id(*hir_id));
991991

992992
match *kind {

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ fn first_non_private<'tcx>(
16501650
}
16511651

16521652
fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type {
1653-
let hir::Ty { hir_id, span, ref kind } = *hir_ty;
1653+
let hir::Ty { hir_id, span, ref kind, source: _ } = *hir_ty;
16541654
let hir::TyKind::Path(qpath) = kind else { unreachable!() };
16551655

16561656
match qpath {

tests/ui/stats/input-stats.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ hir-stats - Wild 72 (NN.N%) 1
9595
hir-stats - Binding 216 (NN.N%) 3
9696
hir-stats GenericParam 400 (NN.N%) 5 80
9797
hir-stats Generics 560 (NN.N%) 10 56
98-
hir-stats Ty 720 (NN.N%) 15 48
99-
hir-stats - Ptr 48 (NN.N%) 1
100-
hir-stats - Ref 48 (NN.N%) 1
101-
hir-stats - Path 624 (NN.N%) 13
10298
hir-stats Expr 768 (NN.N%) 12 64
10399
hir-stats - InlineAsm 64 (NN.N%) 1
104100
hir-stats - Match 64 (NN.N%) 1
105101
hir-stats - Path 64 (NN.N%) 1
106102
hir-stats - Struct 64 (NN.N%) 1
107103
hir-stats - Lit 128 (NN.N%) 2
108104
hir-stats - Block 384 (NN.N%) 6
105+
hir-stats Ty 840 (NN.N%) 15 56
106+
hir-stats - Ptr 56 (NN.N%) 1
107+
hir-stats - Ref 56 (NN.N%) 1
108+
hir-stats - Path 728 (NN.N%) 13
109109
hir-stats Item 968 (NN.N%) 11 88
110110
hir-stats - Enum 88 (NN.N%) 1
111111
hir-stats - ExternCrate 88 (NN.N%) 1
@@ -117,5 +117,5 @@ hir-stats - Use 352 (NN.N%) 4
117117
hir-stats Path 1_040 (NN.N%) 26 40
118118
hir-stats PathSegment 1_776 (NN.N%) 37 48
119119
hir-stats ----------------------------------------------------------------
120-
hir-stats Total 8_644 172
120+
hir-stats Total 8_764 172
121121
hir-stats

0 commit comments

Comments
 (0)