Skip to content

Commit 1bfae6e

Browse files
committed
Extend HIR to track the source of a type
1 parent 640abd7 commit 1bfae6e

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;
@@ -760,6 +759,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
760759
hir_id: self.next_id(),
761760
kind: hir::TyKind::Path(resume_ty),
762761
span: unstable_span,
762+
source: TySource::Other,
763763
};
764764
let inputs = arena_vec![self; input_ty];
765765

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::lints::DelayedLint;
5555
use rustc_hir::{
5656
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem,
57-
LifetimeSource, LifetimeSyntax, ParamName, TraitCandidate,
57+
LifetimeSource, LifetimeSyntax, ParamName, TraitCandidate, TySource,
5858
};
5959
use rustc_index::{Idx, IndexSlice, IndexVec};
6060
use rustc_macros::extension;
@@ -1220,7 +1220,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12201220
bounds,
12211221
TaggedRef::new(lifetime_bound, TraitObjectSyntax::None),
12221222
);
1223-
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1223+
return hir::Ty {
1224+
kind,
1225+
span: self.lower_span(t.span),
1226+
hir_id: self.next_id(),
1227+
source: TySource::Other,
1228+
};
12241229
}
12251230

12261231
let id = self.lower_node_id(t.id);
@@ -1237,7 +1242,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12371242
}
12381243

12391244
fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1240-
hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
1245+
hir::Ty {
1246+
hir_id: self.next_id(),
1247+
kind,
1248+
span: self.lower_span(span),
1249+
source: TySource::Other,
1250+
}
12411251
}
12421252

12431253
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
@@ -1258,7 +1268,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12581268
let lifetime = self.lower_ty_direct_lifetime(t, *region);
12591269
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));
12601270
let span = self.lower_span(t.span);
1261-
let arg = hir::Ty { kind, span, hir_id: self.next_id() };
1271+
let arg = hir::Ty { kind, span, hir_id: self.next_id(), source: TySource::Other };
12621272
let args = self.arena.alloc(hir::GenericArgs {
12631273
args: self.arena.alloc([hir::GenericArg::Type(self.arena.alloc(arg))]),
12641274
constraints: &[],
@@ -1423,7 +1433,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14231433
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
14241434
};
14251435

1426-
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1436+
hir::Ty {
1437+
kind,
1438+
span: self.lower_span(t.span),
1439+
hir_id: self.lower_node_id(t.id),
1440+
source: TySource::Other,
1441+
}
14271442
}
14281443

14291444
fn lower_ty_direct_lifetime(
@@ -2421,7 +2436,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24212436
}
24222437
}
24232438

2424-
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2439+
fn ty_path(&mut self, hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2440+
self.ty_path_with_source(hir_id, span, qpath, TySource::Other)
2441+
}
2442+
2443+
fn ty_path_with_source(
2444+
&mut self,
2445+
mut hir_id: HirId,
2446+
span: Span,
2447+
qpath: hir::QPath<'hir>,
2448+
source: TySource,
2449+
) -> hir::Ty<'hir> {
24252450
let kind = match qpath {
24262451
hir::QPath::Resolved(None, path) => {
24272452
// Turn trait object paths into `TyKind::TraitObject` instead.
@@ -2448,7 +2473,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24482473
_ => hir::TyKind::Path(qpath),
24492474
};
24502475

2451-
hir::Ty { hir_id, kind, span: self.lower_span(span) }
2476+
hir::Ty { hir_id, kind, span: self.lower_span(span), source }
24522477
}
24532478

24542479
/// 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
@@ -499,7 +499,12 @@ pub struct InferArg {
499499

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

@@ -3279,6 +3284,16 @@ impl<'hir> AssocItemConstraintKind<'hir> {
32793284
}
32803285
}
32813286

3287+
#[derive(Debug, Clone, Copy, PartialEq, HashStable_Generic)]
3288+
pub enum TySource {
3289+
/// `Vec` in `Vec::new`
3290+
ImplicitSelf,
3291+
3292+
/// Details not yet needed. Feel free to give useful
3293+
/// categorization to these usages.
3294+
Other,
3295+
}
3296+
32823297
/// An uninhabited enum used to make `Infer` variants on [`Ty`] and [`ConstArg`] be
32833298
/// unreachable. Zero-Variant enums are guaranteed to have the same layout as the never
32843299
/// type.
@@ -3298,6 +3313,7 @@ pub struct Ty<'hir, Unambig = ()> {
32983313
pub hir_id: HirId,
32993314
pub span: Span,
33003315
pub kind: TyKind<'hir, Unambig>,
3316+
pub source: TySource,
33013317
}
33023318

33033319
impl<'hir> Ty<'hir, AmbigArg> {
@@ -4996,7 +5012,7 @@ mod size_asserts {
49965012
static_assert_size!(StmtKind<'_>, 16);
49975013
static_assert_size!(TraitItem<'_>, 88);
49985014
static_assert_size!(TraitItemKind<'_>, 48);
4999-
static_assert_size!(Ty<'_>, 48);
5015+
static_assert_size!(Ty<'_>, 56);
50005016
static_assert_size!(TyKind<'_>, 32);
50015017
// tidy-alphabetical-end
50025018
}

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
@@ -1626,7 +1626,7 @@ fn first_non_private<'tcx>(
16261626
}
16271627

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

16321632
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_676 172
120+
hir-stats Total 8_796 172
121121
hir-stats

0 commit comments

Comments
 (0)