Skip to content

Commit 1c666b7

Browse files
committed
Make impl_trait_ref into a query also returning more information about the impl
1 parent 5f40394 commit 1c666b7

File tree

9 files changed

+48
-40
lines changed

9 files changed

+48
-40
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn provide(providers: &mut Providers) {
7878
trait_def,
7979
adt_def,
8080
fn_sig,
81-
impl_trait_ref,
81+
impl_trait_header,
8282
impl_polarity,
8383
coroutine_kind,
8484
coroutine_for_closure,
@@ -601,7 +601,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
601601
hir::ItemKind::Impl { .. } => {
602602
tcx.ensure().generics_of(def_id);
603603
tcx.ensure().type_of(def_id);
604-
tcx.ensure().impl_trait_ref(def_id);
604+
tcx.ensure().impl_trait_header(def_id);
605605
tcx.ensure().predicates_of(def_id);
606606
}
607607
hir::ItemKind::Trait(..) => {
@@ -1326,19 +1326,20 @@ fn suggest_impl_trait<'tcx>(
13261326
None
13271327
}
13281328

1329-
fn impl_trait_ref(
1329+
fn impl_trait_header(
13301330
tcx: TyCtxt<'_>,
13311331
def_id: LocalDefId,
1332-
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
1332+
) -> Option<(ty::EarlyBinder<ty::TraitRef<'_>>, ty::ImplPolarity)> {
13331333
let icx = ItemCtxt::new(tcx, def_id);
1334-
let impl_ = tcx.hir().expect_item(def_id).expect_impl();
1334+
let item = tcx.hir().expect_item(def_id);
1335+
let impl_ = item.expect_impl();
13351336
impl_
13361337
.of_trait
13371338
.as_ref()
13381339
.map(|ast_trait_ref| {
13391340
let selfty = tcx.type_of(def_id).instantiate_identity();
13401341

1341-
if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
1342+
let impl_trait_ref = if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
13421343
tcx,
13431344
tcx.is_const_trait_impl_raw(def_id.to_def_id()),
13441345
ast_trait_ref,
@@ -1363,9 +1364,9 @@ fn impl_trait_ref(
13631364
icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty)
13641365
} else {
13651366
icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty)
1366-
}
1367+
};
1368+
(ty::EarlyBinder::bind(impl_trait_ref), polarity_of_impl_item(tcx, def_id, impl_, item.span))
13671369
})
1368-
.map(ty::EarlyBinder::bind)
13691370
}
13701371

13711372
fn check_impl_constness(
@@ -1394,42 +1395,38 @@ fn check_impl_constness(
13941395
}
13951396

13961397
fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity {
1397-
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
13981398
let item = tcx.hir().expect_item(def_id);
1399-
match &item.kind {
1400-
hir::ItemKind::Impl(hir::Impl {
1401-
polarity: hir::ImplPolarity::Negative(span),
1402-
of_trait,
1403-
..
1404-
}) => {
1399+
polarity_of_impl_item(tcx, def_id, item.expect_impl(), item.span)
1400+
}
1401+
1402+
fn polarity_of_impl_item(
1403+
tcx: TyCtxt<'_>,
1404+
def_id: LocalDefId,
1405+
impl_: &hir::Impl<'_>,
1406+
span: Span,
1407+
) -> ty::ImplPolarity {
1408+
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1409+
match &impl_ {
1410+
hir::Impl { polarity: hir::ImplPolarity::Negative(span), of_trait, .. } => {
14051411
if is_rustc_reservation {
14061412
let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
14071413
tcx.dcx().span_err(span, "reservation impls can't be negative");
14081414
}
14091415
ty::ImplPolarity::Negative
14101416
}
1411-
hir::ItemKind::Impl(hir::Impl {
1412-
polarity: hir::ImplPolarity::Positive,
1413-
of_trait: None,
1414-
..
1415-
}) => {
1417+
hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => {
14161418
if is_rustc_reservation {
1417-
tcx.dcx().span_err(item.span, "reservation impls can't be inherent");
1419+
tcx.dcx().span_err(span, "reservation impls can't be inherent");
14181420
}
14191421
ty::ImplPolarity::Positive
14201422
}
1421-
hir::ItemKind::Impl(hir::Impl {
1422-
polarity: hir::ImplPolarity::Positive,
1423-
of_trait: Some(_),
1424-
..
1425-
}) => {
1423+
hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: Some(_), .. } => {
14261424
if is_rustc_reservation {
14271425
ty::ImplPolarity::Reservation
14281426
} else {
14291427
ty::ImplPolarity::Positive
14301428
}
14311429
}
1432-
item => bug!("impl_polarity: {:?} not an impl", item),
14331430
}
14341431
}
14351432

compiler/rustc_incremental/src/persist/dirty_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const BASE_HIR: &[&str] = &[
6363

6464
/// `impl` implementation of struct/trait
6565
const BASE_IMPL: &[&str] =
66-
&[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_ref];
66+
&[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
6767

6868
/// DepNodes for mir_built/Optimized, which is relevant in "executable"
6969
/// code, i.e., functions+methods

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ provide! { tcx, def_id, other, cdata,
215215
variances_of => { table }
216216
fn_sig => { table }
217217
codegen_fn_attrs => { table }
218-
impl_trait_ref => { table }
218+
impl_trait_header => { table }
219219
const_param_default => { table }
220220
object_lifetime_default => { table }
221221
thir_abstract_const => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,8 +1964,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19641964
self.tables.defaultness.set_some(def_id.index, tcx.defaultness(def_id));
19651965
self.tables.impl_polarity.set_some(def_id.index, tcx.impl_polarity(def_id));
19661966

1967-
if of_trait && let Some(trait_ref) = tcx.impl_trait_ref(def_id) {
1968-
record!(self.tables.impl_trait_ref[def_id] <- trait_ref);
1967+
if of_trait && let Some(header) = tcx.impl_trait_header(def_id) {
1968+
record!(self.tables.impl_trait_header[def_id] <- header);
1969+
let (trait_ref, _polarity) = header;
19691970

19701971
let trait_ref = trait_ref.instantiate_identity();
19711972
let simplified_self_ty = fast_reject::simplify_type(

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ define_tables! {
423423
variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
424424
fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::PolyFnSig<'static>>>>,
425425
codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
426-
impl_trait_ref: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::TraitRef<'static>>>>,
426+
impl_trait_header: Table<DefIndex, LazyValue<(ty::EarlyBinder<ty::TraitRef<'static>>, ty::ImplPolarity)>>,
427427
const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<rustc_middle::ty::Const<'static>>>>,
428428
object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
429429
optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,

compiler/rustc_middle/src/query/erase.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ impl EraseType for Option<mir::DestructuredConstant<'_>> {
177177
type Result = [u8; size_of::<Option<mir::DestructuredConstant<'static>>>()];
178178
}
179179

180-
impl EraseType for Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
181-
type Result = [u8; size_of::<Option<ty::EarlyBinder<ty::TraitRef<'static>>>>()];
180+
impl EraseType for Option<(ty::EarlyBinder<ty::TraitRef<'_>>, ty::ImplPolarity)> {
181+
type Result =
182+
[u8; size_of::<Option<(ty::EarlyBinder<ty::TraitRef<'static>>, ty::ImplPolarity)>>()];
182183
}
183184

184185
impl EraseType for Option<ty::EarlyBinder<Ty<'_>>> {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,9 @@ rustc_queries! {
846846
cache_on_disk_if { true }
847847
}
848848

849-
/// Given an `impl_id`, return the trait it implements.
849+
/// Given an `impl_id`, return the trait it implements along with some header information.
850850
/// Return `None` if this is an inherent impl.
851-
query impl_trait_ref(impl_id: DefId) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> {
851+
query impl_trait_header(impl_id: DefId) -> Option<(ty::EarlyBinder<ty::TraitRef<'tcx>>, ty::ImplPolarity)> {
852852
desc { |tcx| "computing trait implemented by `{}`", tcx.def_path_str(impl_id) }
853853
cache_on_disk_if { impl_id.is_local() }
854854
separate_provide_extern

compiler/rustc_middle/src/ty/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,15 @@ impl<'tcx> TyCtxt<'tcx> {
23082308
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
23092309
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
23102310
}
2311+
2312+
/// Given an `impl_id`, return the trait it implements.
2313+
/// Return `None` if this is an inherent impl.
2314+
pub fn impl_trait_ref(
2315+
self,
2316+
def_id: impl IntoQueryParam<DefId>,
2317+
) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> {
2318+
Some(self.impl_trait_header(def_id)?.0)
2319+
}
23112320
}
23122321

23132322
/// Parameter attributes that can only be determined by examining the body of a function instead

tests/incremental/hashes/trait_impls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ impl AddTypeParameterToImpl<u32> for Bar<u32> {
462462
}
463463

464464
#[cfg(not(any(cfail1,cfail4)))]
465-
#[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_ref", cfg="cfail2")]
465+
#[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_header", cfg="cfail2")]
466466
#[rustc_clean(cfg="cfail3")]
467-
#[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_ref", cfg="cfail5")]
467+
#[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_header", cfg="cfail5")]
468468
#[rustc_clean(cfg="cfail6")]
469469
impl<TTT> AddTypeParameterToImpl<TTT> for Bar<TTT> {
470470
#[rustc_clean(
@@ -493,9 +493,9 @@ impl ChangeSelfTypeOfImpl for u32 {
493493
}
494494

495495
#[cfg(not(any(cfail1,cfail4)))]
496-
#[rustc_clean(except="opt_hir_owner_nodes,impl_trait_ref", cfg="cfail2")]
496+
#[rustc_clean(except="opt_hir_owner_nodes,impl_trait_header", cfg="cfail2")]
497497
#[rustc_clean(cfg="cfail3")]
498-
#[rustc_clean(except="opt_hir_owner_nodes,impl_trait_ref", cfg="cfail5")]
498+
#[rustc_clean(except="opt_hir_owner_nodes,impl_trait_header", cfg="cfail5")]
499499
#[rustc_clean(cfg="cfail6")]
500500
impl ChangeSelfTypeOfImpl for u64 {
501501
#[rustc_clean(except="fn_sig,typeck,optimized_mir", cfg="cfail2")]

0 commit comments

Comments
 (0)