Skip to content

Commit 979ef59

Browse files
Use LocalDefId in ItemCtxt
1 parent 2eb1c08 commit 979ef59

File tree

11 files changed

+102
-104
lines changed

11 files changed

+102
-104
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub trait AstConv<'tcx> {
7575
fn get_type_parameter_bounds(
7676
&self,
7777
span: Span,
78-
def_id: DefId,
78+
def_id: LocalDefId,
7979
assoc_name: Ident,
8080
) -> ty::GenericPredicates<'tcx>;
8181

@@ -1773,9 +1773,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
17731773
ty_param_def_id, assoc_name, span,
17741774
);
17751775

1776-
let predicates = &self
1777-
.get_type_parameter_bounds(span, ty_param_def_id.to_def_id(), assoc_name)
1778-
.predicates;
1776+
let predicates =
1777+
&self.get_type_parameter_bounds(span, ty_param_def_id, assoc_name).predicates;
17791778

17801779
debug!("find_bound_for_assoc_item: predicates={:#?}", predicates);
17811780

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ fn check_variances_for_type_defn<'tcx>(
17941794

17951795
// Lazily calculated because it is only needed in case of an error.
17961796
let explicitly_bounded_params = LazyCell::new(|| {
1797-
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.to_def_id());
1797+
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.def_id);
17981798
hir_generics
17991799
.predicates
18001800
.iter()

compiler/rustc_hir_analysis/src/collect.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn provide(providers: &mut Providers) {
113113
/// the AST (`hir::Generics`), recursively.
114114
pub struct ItemCtxt<'tcx> {
115115
tcx: TyCtxt<'tcx>,
116-
item_def_id: DefId,
116+
item_def_id: LocalDefId,
117117
}
118118

119119
///////////////////////////////////////////////////////////////////////////
@@ -347,7 +347,7 @@ fn bad_placeholder<'tcx>(
347347
}
348348

349349
impl<'tcx> ItemCtxt<'tcx> {
350-
pub fn new(tcx: TyCtxt<'tcx>, item_def_id: DefId) -> ItemCtxt<'tcx> {
350+
pub fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
351351
ItemCtxt { tcx, item_def_id }
352352
}
353353

@@ -356,7 +356,7 @@ impl<'tcx> ItemCtxt<'tcx> {
356356
}
357357

358358
pub fn hir_id(&self) -> hir::HirId {
359-
self.tcx.hir().local_def_id_to_hir_id(self.item_def_id.expect_local())
359+
self.tcx.hir().local_def_id_to_hir_id(self.item_def_id)
360360
}
361361

362362
pub fn node(&self) -> hir::Node<'tcx> {
@@ -370,20 +370,16 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
370370
}
371371

372372
fn item_def_id(&self) -> DefId {
373-
self.item_def_id
373+
self.item_def_id.to_def_id()
374374
}
375375

376376
fn get_type_parameter_bounds(
377377
&self,
378378
span: Span,
379-
def_id: DefId,
379+
def_id: LocalDefId,
380380
assoc_name: Ident,
381381
) -> ty::GenericPredicates<'tcx> {
382-
self.tcx.at(span).type_param_predicates((
383-
self.item_def_id,
384-
def_id.expect_local(),
385-
assoc_name,
386-
))
382+
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
387383
}
388384

389385
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
@@ -1095,7 +1091,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
10951091

10961092
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
10971093

1098-
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1094+
let icx = ItemCtxt::new(tcx, def_id);
10991095

11001096
let output = match tcx.hir().get(hir_id) {
11011097
TraitItem(hir::TraitItem {
@@ -1136,7 +1132,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
11361132

11371133
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
11381134
let abi = tcx.hir().get_foreign_abi(hir_id);
1139-
compute_sig_of_foreign_fn_decl(tcx, def_id.to_def_id(), fn_decl, abi)
1135+
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
11401136
}
11411137

11421138
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
@@ -1339,7 +1335,7 @@ fn impl_trait_ref(
13391335
tcx: TyCtxt<'_>,
13401336
def_id: LocalDefId,
13411337
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
1342-
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1338+
let icx = ItemCtxt::new(tcx, def_id);
13431339
let impl_ = tcx.hir().expect_item(def_id).expect_impl();
13441340
impl_
13451341
.of_trait
@@ -1465,16 +1461,16 @@ fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicate
14651461

14661462
fn compute_sig_of_foreign_fn_decl<'tcx>(
14671463
tcx: TyCtxt<'tcx>,
1468-
def_id: DefId,
1464+
def_id: LocalDefId,
14691465
decl: &'tcx hir::FnDecl<'tcx>,
14701466
abi: abi::Abi,
14711467
) -> ty::PolyFnSig<'tcx> {
14721468
let unsafety = if abi == abi::Abi::RustIntrinsic {
1473-
intrinsic_operation_unsafety(tcx, def_id)
1469+
intrinsic_operation_unsafety(tcx, def_id.to_def_id())
14741470
} else {
14751471
hir::Unsafety::Unsafe
14761472
};
1477-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1473+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
14781474
let fty =
14791475
ItemCtxt::new(tcx, def_id).astconv().ty_of_fn(hir_id, unsafety, abi, decl, None, None);
14801476

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ use rustc_span::Span;
1616
/// `hr-associated-type-bound-1.rs`.
1717
fn associated_type_bounds<'tcx>(
1818
tcx: TyCtxt<'tcx>,
19-
assoc_item_def_id: DefId,
19+
assoc_item_def_id: LocalDefId,
2020
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
2121
span: Span,
2222
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
2323
let item_ty = tcx.mk_projection(
24-
assoc_item_def_id,
25-
InternalSubsts::identity_for_item(tcx, assoc_item_def_id),
24+
assoc_item_def_id.to_def_id(),
25+
InternalSubsts::identity_for_item(tcx, assoc_item_def_id.to_def_id()),
2626
);
2727

2828
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
2929
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
3030
// Associated types are implicitly sized unless a `?Sized` bound is found
3131
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
3232

33-
let trait_def_id = tcx.parent(assoc_item_def_id);
34-
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id.expect_local());
33+
let trait_def_id = tcx.local_parent(assoc_item_def_id);
34+
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);
3535

3636
let bounds_from_parent = trait_predicates.predicates.iter().copied().filter(|(pred, _)| {
3737
match pred.kind().skip_binder() {
@@ -45,7 +45,11 @@ fn associated_type_bounds<'tcx>(
4545
});
4646

4747
let all_bounds = tcx.arena.alloc_from_iter(bounds.predicates().chain(bounds_from_parent));
48-
debug!("associated_type_bounds({}) = {:?}", tcx.def_path_str(assoc_item_def_id), all_bounds);
48+
debug!(
49+
"associated_type_bounds({}) = {:?}",
50+
tcx.def_path_str(assoc_item_def_id.to_def_id()),
51+
all_bounds
52+
);
4953
all_bounds
5054
}
5155

@@ -56,7 +60,7 @@ fn associated_type_bounds<'tcx>(
5660
#[instrument(level = "trace", skip(tcx), ret)]
5761
fn opaque_type_bounds<'tcx>(
5862
tcx: TyCtxt<'tcx>,
59-
opaque_def_id: DefId,
63+
opaque_def_id: LocalDefId,
6064
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
6165
item_ty: Ty<'tcx>,
6266
span: Span,
@@ -84,7 +88,7 @@ pub(super) fn explicit_item_bounds(
8488
let opaque_ty = item.expect_opaque_ty();
8589
return opaque_type_bounds(
8690
tcx,
87-
opaque_def_id,
91+
opaque_def_id.expect_local(),
8892
opaque_ty.bounds,
8993
tcx.mk_projection(
9094
def_id.to_def_id(),
@@ -104,7 +108,7 @@ pub(super) fn explicit_item_bounds(
104108
kind: hir::TraitItemKind::Type(bounds, _),
105109
span,
106110
..
107-
}) => associated_type_bounds(tcx, def_id.to_def_id(), bounds, *span),
111+
}) => associated_type_bounds(tcx, def_id, bounds, *span),
108112
hir::Node::Item(hir::Item {
109113
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait, .. }),
110114
span,
@@ -116,7 +120,7 @@ pub(super) fn explicit_item_bounds(
116120
} else {
117121
tcx.mk_opaque(def_id.to_def_id(), substs)
118122
};
119-
opaque_type_bounds(tcx, def_id.to_def_id(), bounds, item_ty, *span)
123+
opaque_type_bounds(tcx, def_id, bounds, item_ty, *span)
120124
}
121125
_ => bug!("item_bounds called on {:?}", def_id),
122126
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+60-61
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
7272
let mut is_default_impl_trait = None;
7373

7474
// FIXME: Should ItemCtxt take a LocalDefId?
75-
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
75+
let icx = ItemCtxt::new(tcx, def_id);
7676

7777
const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
7878

@@ -551,80 +551,78 @@ pub(super) fn super_predicates_that_define_assoc_type(
551551
tcx: TyCtxt<'_>,
552552
(trait_def_id, assoc_name): (DefId, Option<Ident>),
553553
) -> ty::GenericPredicates<'_> {
554-
if trait_def_id.is_local() {
555-
debug!("local trait");
556-
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
554+
let Some(trait_def_id) = trait_def_id.as_local() else {
555+
// if `assoc_name` is None, then the query should've been redirected to an
556+
// external provider
557+
assert!(assoc_name.is_some());
558+
return tcx.super_predicates_of(trait_def_id);
559+
};
557560

558-
let Node::Item(item) = tcx.hir().get(trait_hir_id) else {
559-
bug!("trait_node_id {} is not an item", trait_hir_id);
560-
};
561+
debug!("local trait");
562+
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id);
561563

562-
let (generics, bounds) = match item.kind {
563-
hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
564-
hir::ItemKind::TraitAlias(generics, supertraits) => (generics, supertraits),
565-
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
566-
};
564+
let Node::Item(item) = tcx.hir().get(trait_hir_id) else {
565+
bug!("trait_node_id {} is not an item", trait_hir_id);
566+
};
567567

568-
let icx = ItemCtxt::new(tcx, trait_def_id);
568+
let (generics, bounds) = match item.kind {
569+
hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
570+
hir::ItemKind::TraitAlias(generics, supertraits) => (generics, supertraits),
571+
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
572+
};
569573

570-
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
571-
let self_param_ty = tcx.types.self_param;
572-
let superbounds1 = if let Some(assoc_name) = assoc_name {
573-
icx.astconv().compute_bounds_that_match_assoc_type(self_param_ty, bounds, assoc_name)
574-
} else {
575-
icx.astconv().compute_bounds(self_param_ty, bounds)
576-
};
574+
let icx = ItemCtxt::new(tcx, trait_def_id);
577575

578-
let superbounds1 = superbounds1.predicates();
579-
580-
// Convert any explicit superbounds in the where-clause,
581-
// e.g., `trait Foo where Self: Bar`.
582-
// In the case of trait aliases, however, we include all bounds in the where-clause,
583-
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
584-
// as one of its "superpredicates".
585-
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
586-
let superbounds2 = icx.type_parameter_bounds_in_generics(
587-
generics,
588-
item.owner_id.def_id,
589-
self_param_ty,
590-
OnlySelfBounds(!is_trait_alias),
591-
assoc_name,
592-
);
576+
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
577+
let self_param_ty = tcx.types.self_param;
578+
let superbounds1 = if let Some(assoc_name) = assoc_name {
579+
icx.astconv().compute_bounds_that_match_assoc_type(self_param_ty, bounds, assoc_name)
580+
} else {
581+
icx.astconv().compute_bounds(self_param_ty, bounds)
582+
};
583+
584+
let superbounds1 = superbounds1.predicates();
585+
586+
// Convert any explicit superbounds in the where-clause,
587+
// e.g., `trait Foo where Self: Bar`.
588+
// In the case of trait aliases, however, we include all bounds in the where-clause,
589+
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
590+
// as one of its "superpredicates".
591+
let is_trait_alias = tcx.is_trait_alias(trait_def_id.to_def_id());
592+
let superbounds2 = icx.type_parameter_bounds_in_generics(
593+
generics,
594+
item.owner_id.def_id,
595+
self_param_ty,
596+
OnlySelfBounds(!is_trait_alias),
597+
assoc_name,
598+
);
593599

594-
// Combine the two lists to form the complete set of superbounds:
595-
let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
596-
debug!(?superbounds);
600+
// Combine the two lists to form the complete set of superbounds:
601+
let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
602+
debug!(?superbounds);
597603

604+
// Now require that immediate supertraits are converted,
605+
// which will, in turn, reach indirect supertraits.
606+
if assoc_name.is_none() {
598607
// Now require that immediate supertraits are converted,
599608
// which will, in turn, reach indirect supertraits.
600-
if assoc_name.is_none() {
601-
// Now require that immediate supertraits are converted,
602-
// which will, in turn, reach indirect supertraits.
603-
for &(pred, span) in superbounds {
604-
debug!("superbound: {:?}", pred);
605-
if let ty::PredicateKind::Clause(ty::Clause::Trait(bound)) =
606-
pred.kind().skip_binder()
607-
{
608-
tcx.at(span).super_predicates_of(bound.def_id());
609-
}
609+
for &(pred, span) in superbounds {
610+
debug!("superbound: {:?}", pred);
611+
if let ty::PredicateKind::Clause(ty::Clause::Trait(bound)) = pred.kind().skip_binder() {
612+
tcx.at(span).super_predicates_of(bound.def_id());
610613
}
611614
}
612-
613-
ty::GenericPredicates { parent: None, predicates: superbounds }
614-
} else {
615-
// if `assoc_name` is None, then the query should've been redirected to an
616-
// external provider
617-
assert!(assoc_name.is_some());
618-
tcx.super_predicates_of(trait_def_id)
619615
}
616+
617+
ty::GenericPredicates { parent: None, predicates: superbounds }
620618
}
621619

622620
/// Returns the predicates defined on `item_def_id` of the form
623621
/// `X: Foo` where `X` is the type parameter `def_id`.
624622
#[instrument(level = "trace", skip(tcx))]
625623
pub(super) fn type_param_predicates(
626624
tcx: TyCtxt<'_>,
627-
(item_def_id, def_id, assoc_name): (DefId, LocalDefId, Ident),
625+
(item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident),
628626
) -> ty::GenericPredicates<'_> {
629627
use rustc_hir::*;
630628

@@ -639,21 +637,21 @@ pub(super) fn type_param_predicates(
639637
let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id));
640638

641639
// Don't look for bounds where the type parameter isn't in scope.
642-
let parent = if item_def_id == param_owner.to_def_id() {
640+
let parent = if item_def_id == param_owner {
643641
None
644642
} else {
645-
tcx.generics_of(item_def_id).parent
643+
tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
646644
};
647645

648646
let mut result = parent
649647
.map(|parent| {
650648
let icx = ItemCtxt::new(tcx, parent);
651-
icx.get_type_parameter_bounds(DUMMY_SP, def_id.to_def_id(), assoc_name)
649+
icx.get_type_parameter_bounds(DUMMY_SP, def_id, assoc_name)
652650
})
653651
.unwrap_or_default();
654652
let mut extend = None;
655653

656-
let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
654+
let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id);
657655
let ast_generics = match tcx.hir().get(item_hir_id) {
658656
Node::TraitItem(item) => &item.generics,
659657

@@ -675,7 +673,8 @@ pub(super) fn type_param_predicates(
675673
ItemKind::Trait(_, _, generics, ..) => {
676674
// Implied `Self: Trait` and supertrait bounds.
677675
if param_id == item_hir_id {
678-
let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id);
676+
let identity_trait_ref =
677+
ty::TraitRef::identity(tcx, item_def_id.to_def_id());
679678
extend =
680679
Some((identity_trait_ref.without_const().to_predicate(tcx), item.span));
681680
}

0 commit comments

Comments
 (0)