Skip to content

Commit 916951e

Browse files
committed
Make impl_trait_ref into a query also returning more information about the impl
1 parent de4d615 commit 916951e

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,
@@ -598,7 +598,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
598598
hir::ItemKind::Impl { .. } => {
599599
tcx.ensure().generics_of(def_id);
600600
tcx.ensure().type_of(def_id);
601-
tcx.ensure().impl_trait_ref(def_id);
601+
tcx.ensure().impl_trait_header(def_id);
602602
tcx.ensure().predicates_of(def_id);
603603
}
604604
hir::ItemKind::Trait(..) => {
@@ -1323,19 +1323,20 @@ fn suggest_impl_trait<'tcx>(
13231323
None
13241324
}
13251325

1326-
fn impl_trait_ref(
1326+
fn impl_trait_header(
13271327
tcx: TyCtxt<'_>,
13281328
def_id: LocalDefId,
1329-
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
1329+
) -> Option<(ty::EarlyBinder<ty::TraitRef<'_>>, ty::ImplPolarity)> {
13301330
let icx = ItemCtxt::new(tcx, def_id);
1331-
let impl_ = tcx.hir().expect_item(def_id).expect_impl();
1331+
let item = tcx.hir().expect_item(def_id);
1332+
let impl_ = item.expect_impl();
13321333
impl_
13331334
.of_trait
13341335
.as_ref()
13351336
.map(|ast_trait_ref| {
13361337
let selfty = tcx.type_of(def_id).instantiate_identity();
13371338

1338-
if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
1339+
let impl_trait_ref = if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
13391340
tcx,
13401341
tcx.is_const_trait_impl_raw(def_id.to_def_id()),
13411342
ast_trait_ref,
@@ -1360,9 +1361,9 @@ fn impl_trait_ref(
13601361
icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty)
13611362
} else {
13621363
icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty)
1363-
}
1364+
};
1365+
(ty::EarlyBinder::bind(impl_trait_ref), polarity_of_impl(tcx, def_id, impl_, item.span))
13641366
})
1365-
.map(ty::EarlyBinder::bind)
13661367
}
13671368

13681369
fn check_impl_constness(
@@ -1391,42 +1392,38 @@ fn check_impl_constness(
13911392
}
13921393

13931394
fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity {
1394-
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
13951395
let item = tcx.hir().expect_item(def_id);
1396-
match &item.kind {
1397-
hir::ItemKind::Impl(hir::Impl {
1398-
polarity: hir::ImplPolarity::Negative(span),
1399-
of_trait,
1400-
..
1401-
}) => {
1396+
polarity_of_impl(tcx, def_id, item.expect_impl(), item.span)
1397+
}
1398+
1399+
fn polarity_of_impl(
1400+
tcx: TyCtxt<'_>,
1401+
def_id: LocalDefId,
1402+
impl_: &hir::Impl<'_>,
1403+
span: Span,
1404+
) -> ty::ImplPolarity {
1405+
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1406+
match &impl_ {
1407+
hir::Impl { polarity: hir::ImplPolarity::Negative(span), of_trait, .. } => {
14021408
if is_rustc_reservation {
14031409
let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
14041410
tcx.dcx().span_err(span, "reservation impls can't be negative");
14051411
}
14061412
ty::ImplPolarity::Negative
14071413
}
1408-
hir::ItemKind::Impl(hir::Impl {
1409-
polarity: hir::ImplPolarity::Positive,
1410-
of_trait: None,
1411-
..
1412-
}) => {
1414+
hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => {
14131415
if is_rustc_reservation {
1414-
tcx.dcx().span_err(item.span, "reservation impls can't be inherent");
1416+
tcx.dcx().span_err(span, "reservation impls can't be inherent");
14151417
}
14161418
ty::ImplPolarity::Positive
14171419
}
1418-
hir::ItemKind::Impl(hir::Impl {
1419-
polarity: hir::ImplPolarity::Positive,
1420-
of_trait: Some(_),
1421-
..
1422-
}) => {
1420+
hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: Some(_), .. } => {
14231421
if is_rustc_reservation {
14241422
ty::ImplPolarity::Reservation
14251423
} else {
14261424
ty::ImplPolarity::Positive
14271425
}
14281426
}
1429-
item => bug!("impl_polarity: {:?} not an impl", item),
14301427
}
14311428
}
14321429

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
@@ -1971,8 +1971,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19711971
self.tables.defaultness.set_some(def_id.index, tcx.defaultness(def_id));
19721972
self.tables.impl_polarity.set_some(def_id.index, tcx.impl_polarity(def_id));
19731973

1974-
if of_trait && let Some(trait_ref) = tcx.impl_trait_ref(def_id) {
1975-
record!(self.tables.impl_trait_ref[def_id] <- trait_ref);
1974+
if of_trait && let Some(header) = tcx.impl_trait_header(def_id) {
1975+
record!(self.tables.impl_trait_header[def_id] <- header);
1976+
let (trait_ref, _polarity) = header;
19761977

19771978
let trait_ref = trait_ref.instantiate_identity();
19781979
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)