Skip to content

Commit 51f875f

Browse files
committed
Extend HIR to track the source of a type
1 parent e712fbd commit 51f875f

File tree

8 files changed

+80
-31
lines changed

8 files changed

+80
-31
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

+3
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};
@@ -173,12 +174,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
173174
hir_id: self.next_id(),
174175
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Input(arg)),
175176
span,
177+
source: TySource::Other,
176178
}));
177179

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

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

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use rustc_ast::ptr::P as AstP;
66
use rustc_ast::*;
77
use rustc_ast_pretty::pprust::expr_to_string;
88
use rustc_data_structures::stack::ensure_sufficient_stack;
9-
use rustc_hir as hir;
10-
use rustc_hir::HirId;
119
use rustc_hir::def::{DefKind, Res};
10+
use rustc_hir::{self as hir, HirId, TySource};
1211
use rustc_middle::span_bug;
1312
use rustc_middle::ty::TyCtxt;
1413
use rustc_session::errors::report_lit_error;
@@ -753,6 +752,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
753752
hir_id: self.next_id(),
754753
kind: hir::TyKind::Path(resume_ty),
755754
span: unstable_span,
755+
source: TySource::Other,
756756
};
757757
let inputs = arena_vec![self; input_ty];
758758

compiler/rustc_ast_lowering/src/lib.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5656
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5757
use rustc_hir::{
5858
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, LifetimeSource,
59-
LifetimeSyntax, ParamName, TraitCandidate,
59+
LifetimeSyntax, ParamName, TraitCandidate, TySource,
6060
};
6161
use rustc_index::{Idx, IndexSlice, IndexVec};
6262
use rustc_macros::extension;
@@ -1172,7 +1172,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11721172
bounds,
11731173
TaggedRef::new(lifetime_bound, TraitObjectSyntax::None),
11741174
);
1175-
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1175+
return hir::Ty {
1176+
kind,
1177+
span: self.lower_span(t.span),
1178+
hir_id: self.next_id(),
1179+
source: TySource::Other,
1180+
};
11761181
}
11771182

11781183
let id = self.lower_node_id(t.id);
@@ -1189,7 +1194,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11891194
}
11901195

11911196
fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1192-
hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
1197+
hir::Ty {
1198+
hir_id: self.next_id(),
1199+
kind,
1200+
span: self.lower_span(span),
1201+
source: TySource::Other,
1202+
}
11931203
}
11941204

11951205
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
@@ -1210,7 +1220,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12101220
let lifetime = self.lower_ty_direct_lifetime(t, *region);
12111221
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));
12121222
let span = self.lower_span(t.span);
1213-
let arg = hir::Ty { kind, span, hir_id: self.next_id() };
1223+
let arg = hir::Ty { kind, span, hir_id: self.next_id(), source: TySource::Other };
12141224
let args = self.arena.alloc(hir::GenericArgs {
12151225
args: self.arena.alloc([hir::GenericArg::Type(self.arena.alloc(arg))]),
12161226
constraints: &[],
@@ -1375,7 +1385,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13751385
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
13761386
};
13771387

1378-
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1388+
hir::Ty {
1389+
kind,
1390+
span: self.lower_span(t.span),
1391+
hir_id: self.lower_node_id(t.id),
1392+
source: TySource::Other,
1393+
}
13791394
}
13801395

13811396
fn lower_ty_direct_lifetime(
@@ -2378,7 +2393,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23782393
}
23792394
}
23802395

2381-
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2396+
fn ty_path(&mut self, hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2397+
self.ty_path_with_source(hir_id, span, qpath, TySource::Other)
2398+
}
2399+
2400+
fn ty_path_with_source(
2401+
&mut self,
2402+
mut hir_id: HirId,
2403+
span: Span,
2404+
qpath: hir::QPath<'hir>,
2405+
source: TySource,
2406+
) -> hir::Ty<'hir> {
23822407
let kind = match qpath {
23832408
hir::QPath::Resolved(None, path) => {
23842409
// Turn trait object paths into `TyKind::TraitObject` instead.
@@ -2405,7 +2430,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24052430
_ => hir::TyKind::Path(qpath),
24062431
};
24072432

2408-
hir::Ty { hir_id, kind, span: self.lower_span(span) }
2433+
hir::Ty { hir_id, kind, span: self.lower_span(span), source }
24092434
}
24102435

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

compiler/rustc_ast_lowering/src/path.rs

+7-2
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, 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;
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

+18-2
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,12 @@ pub struct InferArg {
473473

474474
impl InferArg {
475475
pub fn to_ty(&self) -> Ty<'static> {
476-
Ty { kind: TyKind::Infer(()), span: self.span, hir_id: self.hir_id }
476+
Ty {
477+
kind: TyKind::Infer(()),
478+
span: self.span,
479+
hir_id: self.hir_id,
480+
source: TySource::Other,
481+
}
477482
}
478483
}
479484

@@ -3220,6 +3225,16 @@ impl<'hir> AssocItemConstraintKind<'hir> {
32203225
}
32213226
}
32223227

3228+
#[derive(Debug, Clone, Copy, PartialEq, HashStable_Generic)]
3229+
pub enum TySource {
3230+
/// `Vec` in `Vec::new`
3231+
ImplicitSelf,
3232+
3233+
/// Details not yet needed. Feel free to give useful
3234+
/// categorization to these usages.
3235+
Other,
3236+
}
3237+
32233238
/// An uninhabited enum used to make `Infer` variants on [`Ty`] and [`ConstArg`] be
32243239
/// unreachable. Zero-Variant enums are guaranteed to have the same layout as the never
32253240
/// type.
@@ -3239,6 +3254,7 @@ pub struct Ty<'hir, Unambig = ()> {
32393254
pub hir_id: HirId,
32403255
pub span: Span,
32413256
pub kind: TyKind<'hir, Unambig>,
3257+
pub source: TySource,
32423258
}
32433259

32443260
impl<'hir> Ty<'hir, AmbigArg> {
@@ -4937,7 +4953,7 @@ mod size_asserts {
49374953
static_assert_size!(StmtKind<'_>, 16);
49384954
static_assert_size!(TraitItem<'_>, 88);
49394955
static_assert_size!(TraitItemKind<'_>, 48);
4940-
static_assert_size!(Ty<'_>, 48);
4956+
static_assert_size!(Ty<'_>, 56);
49414957
static_assert_size!(TyKind<'_>, 32);
49424958
// tidy-alphabetical-end
49434959
}

compiler/rustc_hir/src/hir/tests.rs

+3-3
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 },

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ fn first_non_private<'tcx>(
16771677
}
16781678

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

16831683
match qpath {

tests/ui/stats/input-stats.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -143,36 +143,36 @@ hir-stats Attribute 128 ( 1.4%) 4 32
143143
hir-stats FieldDef 128 ( 1.4%) 2 64
144144
hir-stats GenericArgs 144 ( 1.6%) 3 48
145145
hir-stats Variant 144 ( 1.6%) 2 72
146-
hir-stats GenericBound 256 ( 2.9%) 4 64
147-
hir-stats - Trait 256 ( 2.9%) 4
146+
hir-stats GenericBound 256 ( 2.8%) 4 64
147+
hir-stats - Trait 256 ( 2.8%) 4
148148
hir-stats Block 288 ( 3.2%) 6 48
149149
hir-stats Pat 360 ( 4.0%) 5 72
150150
hir-stats - Struct 72 ( 0.8%) 1
151151
hir-stats - Wild 72 ( 0.8%) 1
152152
hir-stats - Binding 216 ( 2.4%) 3
153-
hir-stats GenericParam 400 ( 4.5%) 5 80
153+
hir-stats GenericParam 400 ( 4.4%) 5 80
154154
hir-stats Generics 560 ( 6.2%) 10 56
155-
hir-stats Ty 720 ( 8.0%) 15 48
156-
hir-stats - Ptr 48 ( 0.5%) 1
157-
hir-stats - Ref 48 ( 0.5%) 1
158-
hir-stats - Path 624 ( 6.9%) 13
159-
hir-stats Expr 768 ( 8.6%) 12 64
155+
hir-stats Expr 768 ( 8.4%) 12 64
160156
hir-stats - InlineAsm 64 ( 0.7%) 1
161157
hir-stats - Match 64 ( 0.7%) 1
162158
hir-stats - Path 64 ( 0.7%) 1
163159
hir-stats - Struct 64 ( 0.7%) 1
164160
hir-stats - Lit 128 ( 1.4%) 2
165-
hir-stats - Block 384 ( 4.3%) 6
166-
hir-stats Item 968 (10.8%) 11 88
161+
hir-stats - Block 384 ( 4.2%) 6
162+
hir-stats Ty 840 ( 9.2%) 15 56
163+
hir-stats - Ptr 56 ( 0.6%) 1
164+
hir-stats - Ref 56 ( 0.6%) 1
165+
hir-stats - Path 728 ( 8.0%) 13
166+
hir-stats Item 968 (10.6%) 11 88
167167
hir-stats - Enum 88 ( 1.0%) 1
168168
hir-stats - ExternCrate 88 ( 1.0%) 1
169169
hir-stats - ForeignMod 88 ( 1.0%) 1
170170
hir-stats - Impl 88 ( 1.0%) 1
171171
hir-stats - Trait 88 ( 1.0%) 1
172-
hir-stats - Fn 176 ( 2.0%) 2
172+
hir-stats - Fn 176 ( 1.9%) 2
173173
hir-stats - Use 352 ( 3.9%) 4
174-
hir-stats Path 1_240 (13.8%) 31 40
175-
hir-stats PathSegment 1_920 (21.4%) 40 48
174+
hir-stats Path 1_240 (13.6%) 31 40
175+
hir-stats PathSegment 1_920 (21.1%) 40 48
176176
hir-stats ----------------------------------------------------------------
177-
hir-stats Total 8_980 180
177+
hir-stats Total 9_100 180
178178
hir-stats

0 commit comments

Comments
 (0)