Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a2accc

Browse files
Auto merge of #144607 - camsteffen:impl-trait-header-option, r=<try>
Limit impl_trait_header query to only trait impls
2 parents e5e79f8 + 87f803c commit 0a2accc

File tree

75 files changed

+267
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+267
-269
lines changed

‎compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
682682
}
683683
let my_def = self.body.source.def_id();
684684
let Some(td) =
685-
self.infcx.tcx.impl_of_assoc(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
685+
self.infcx.tcx.impl_of_assoc(my_def).and_then(|x| self.infcx.tcx.impl_opt_trait_id(x))
686686
else {
687687
return (false, false, None);
688688
};

‎compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
535535
// subroutine's self-type.
536536
if let Some(impl_def_id) = cx.tcx.impl_of_assoc(instance.def_id()) {
537537
// If the method does *not* belong to a trait, proceed
538-
if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
538+
if !cx.tcx.impl_is_of_trait(impl_def_id) {
539539
let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(
540540
instance.args,
541541
cx.typing_env(),

‎compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::TyCtxt;
77
fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
88
let parent_id = tcx.local_parent(def_id);
99
match tcx.def_kind(parent_id) {
10-
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).unwrap().constness,
10+
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness,
1111
DefKind::Trait => {
1212
if tcx.is_const_trait(parent_id.into()) {
1313
hir::Constness::Const

‎compiler/rustc_hir_analysis/src/check/always_applicable.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ fn ensure_impl_params_and_item_params_correspond<'tcx>(
148148
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
149149
ty::ImplPolarity::Negative => "!",
150150
};
151-
let trait_name = tcx
152-
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
151+
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
153152
let mut err = struct_span_code_err!(
154153
tcx.dcx(),
155154
impl_span,
@@ -187,8 +186,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
187186
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
188187

189188
let impl_span = tcx.def_span(impl_def_id.to_def_id());
190-
let trait_name = tcx
191-
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
189+
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
192190
let polarity = match tcx.impl_polarity(impl_def_id) {
193191
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
194192
ty::ImplPolarity::Negative => "!",
@@ -212,8 +210,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
212210
ty::EarlyBinder::bind(tcx.param_env(adt_def_id)).instantiate(tcx, adt_to_impl_args);
213211

214212
let fresh_impl_args = infcx.fresh_args_for_item(impl_span, impl_def_id.to_def_id());
215-
let fresh_adt_ty =
216-
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate(tcx, fresh_impl_args).self_ty();
213+
let fresh_adt_ty = tcx.impl_trait_ref(impl_def_id).instantiate(tcx, fresh_impl_args).self_ty();
217214

218215
ocx.eq(&ObligationCause::dummy_with_span(impl_span), adt_env, fresh_adt_ty, impl_adt_ty)
219216
.expect("equating fully generic trait ref should never fail");

‎compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,10 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
806806
DefKind::Impl { of_trait } => {
807807
tcx.ensure_ok().generics_of(def_id);
808808
tcx.ensure_ok().type_of(def_id);
809-
tcx.ensure_ok().impl_trait_header(def_id);
810809
tcx.ensure_ok().predicates_of(def_id);
811810
tcx.ensure_ok().associated_items(def_id);
812-
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
811+
if of_trait {
812+
let impl_trait_header = tcx.impl_trait_header(def_id);
813813
res = res.and(
814814
tcx.ensure_ok()
815815
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id),
@@ -1194,9 +1194,7 @@ fn check_impl_items_against_trait<'tcx>(
11941194
tcx,
11951195
ty_impl_item,
11961196
ty_trait_item,
1197-
tcx.impl_trait_ref(ty_impl_item.container_id(tcx))
1198-
.unwrap()
1199-
.instantiate_identity(),
1197+
tcx.impl_trait_ref(ty_impl_item.container_id(tcx)).instantiate_identity(),
12001198
);
12011199
}
12021200
ty::AssocKind::Const { .. } => {}

‎compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ pub(super) fn compare_impl_item(
3838
) -> Result<(), ErrorGuaranteed> {
3939
let impl_item = tcx.associated_item(impl_item_def_id);
4040
let trait_item = tcx.associated_item(impl_item.trait_item_def_id.unwrap());
41-
let impl_trait_ref =
42-
tcx.impl_trait_ref(impl_item.container_id(tcx)).unwrap().instantiate_identity();
41+
let impl_trait_ref = tcx.impl_trait_ref(impl_item.container_id(tcx)).instantiate_identity();
4342
debug!(?impl_trait_ref);
4443

4544
match impl_item.kind {
@@ -445,10 +444,9 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
445444
tcx: TyCtxt<'tcx>,
446445
impl_m_def_id: LocalDefId,
447446
) -> Result<&'tcx DefIdMap<ty::EarlyBinder<'tcx, Ty<'tcx>>>, ErrorGuaranteed> {
448-
let impl_m = tcx.opt_associated_item(impl_m_def_id.to_def_id()).unwrap();
449-
let trait_m = tcx.opt_associated_item(impl_m.trait_item_def_id.unwrap()).unwrap();
450-
let impl_trait_ref =
451-
tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap().instantiate_identity();
447+
let impl_m = tcx.associated_item(impl_m_def_id.to_def_id());
448+
let trait_m = tcx.associated_item(impl_m.trait_item_def_id.unwrap());
449+
let impl_trait_ref = tcx.impl_trait_ref(tcx.parent(impl_m.def_id)).instantiate_identity();
452450
// First, check a few of the same things as `compare_impl_method`,
453451
// just so we don't ICE during instantiation later.
454452
check_method_is_structurally_compatible(tcx, impl_m, trait_m, impl_trait_ref, true)?;

‎compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn missing_items_err(
241241
let snippet = with_types_for_signature!(suggestion_signature(
242242
tcx,
243243
trait_item,
244-
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity(),
244+
tcx.impl_trait_ref(impl_def_id).instantiate_identity(),
245245
));
246246
let code = format!("{padding}{snippet}\n{padding}");
247247
if let Some(span) = tcx.hir_span_if_local(trait_item.def_id) {

‎compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -245,47 +245,49 @@ pub(super) fn check_item<'tcx>(
245245
// won't be allowed unless there's an *explicit* implementation of `Send`
246246
// for `T`
247247
hir::ItemKind::Impl(impl_) => {
248-
let header = tcx.impl_trait_header(def_id);
249-
let is_auto = header
250-
.is_some_and(|header| tcx.trait_is_auto(header.trait_ref.skip_binder().def_id));
251-
252-
crate::impl_wf_check::check_impl_wf(tcx, def_id)?;
248+
crate::impl_wf_check::check_impl_wf(tcx, def_id, impl_.of_trait.is_some())?;
253249
let mut res = Ok(());
254-
if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
255-
let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
256-
res = Err(tcx
257-
.dcx()
258-
.struct_span_err(sp, "impls of auto traits cannot be default")
259-
.with_span_labels(impl_.defaultness_span, "default because of this")
260-
.with_span_label(sp, "auto trait")
261-
.emit());
262-
}
263-
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
264-
match header.map(|h| h.polarity) {
265-
// `None` means this is an inherent impl
266-
Some(ty::ImplPolarity::Positive) | None => {
267-
res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait));
268-
}
269-
Some(ty::ImplPolarity::Negative) => {
270-
let ast::ImplPolarity::Negative(span) = impl_.polarity else {
271-
bug!("impl_polarity query disagrees with impl's polarity in HIR");
272-
};
273-
// FIXME(#27579): what amount of WF checking do we need for neg impls?
274-
if let hir::Defaultness::Default { .. } = impl_.defaultness {
275-
let mut spans = vec![span];
276-
spans.extend(impl_.defaultness_span);
277-
res = Err(struct_span_code_err!(
278-
tcx.dcx(),
279-
spans,
280-
E0750,
281-
"negative impls cannot be default impls"
282-
)
250+
if impl_.of_trait.is_some() {
251+
let header = tcx.impl_trait_header(def_id);
252+
let is_auto = tcx.trait_is_auto(header.trait_ref.skip_binder().def_id);
253+
if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
254+
let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
255+
res = Err(tcx
256+
.dcx()
257+
.struct_span_err(sp, "impls of auto traits cannot be default")
258+
.with_span_labels(impl_.defaultness_span, "default because of this")
259+
.with_span_label(sp, "auto trait")
283260
.emit());
284-
}
285261
}
286-
Some(ty::ImplPolarity::Reservation) => {
287-
// FIXME: what amount of WF checking do we need for reservation impls?
262+
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
263+
match header.polarity {
264+
// `None` means this is an inherent impl
265+
ty::ImplPolarity::Positive => {
266+
res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait));
267+
}
268+
ty::ImplPolarity::Negative => {
269+
let ast::ImplPolarity::Negative(span) = impl_.polarity else {
270+
bug!("impl_polarity query disagrees with impl's polarity in HIR");
271+
};
272+
// FIXME(#27579): what amount of WF checking do we need for neg impls?
273+
if let hir::Defaultness::Default { .. } = impl_.defaultness {
274+
let mut spans = vec![span];
275+
spans.extend(impl_.defaultness_span);
276+
res = Err(struct_span_code_err!(
277+
tcx.dcx(),
278+
spans,
279+
E0750,
280+
"negative impls cannot be default impls"
281+
)
282+
.emit());
283+
}
284+
}
285+
ty::ImplPolarity::Reservation => {
286+
// FIXME: what amount of WF checking do we need for reservation impls?
287+
}
288288
}
289+
} else {
290+
res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait));
289291
}
290292
res
291293
}
@@ -1271,7 +1273,7 @@ fn check_impl<'tcx>(
12711273
// `#[rustc_reservation_impl]` impls are not real impls and
12721274
// therefore don't need to be WF (the trait's `Self: Trait` predicate
12731275
// won't hold).
1274-
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
1276+
let trait_ref = tcx.impl_trait_ref(item.owner_id).instantiate_identity();
12751277
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
12761278
// other `Foo` impls are incoherent.
12771279
tcx.ensure_ok().coherent_trait(trait_ref.def_id)?;

‎compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ pub(crate) fn coerce_unsized_info<'tcx>(
386386
let unsize_trait = tcx.require_lang_item(LangItem::Unsize, span);
387387

388388
let source = tcx.type_of(impl_did).instantiate_identity();
389-
let trait_ref = tcx.impl_trait_ref(impl_did).unwrap().instantiate_identity();
389+
let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity();
390390

391391
assert_eq!(trait_ref.def_id, coerce_unsized_trait);
392392
let target = trait_ref.args.type_at(1);
@@ -714,7 +714,7 @@ fn visit_implementation_of_coerce_pointee_validity(
714714
checker: &Checker<'_>,
715715
) -> Result<(), ErrorGuaranteed> {
716716
let tcx = checker.tcx;
717-
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).unwrap().instantiate_identity().self_ty();
717+
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).instantiate_identity().self_ty();
718718
let span = tcx.def_span(checker.impl_def_id);
719719
if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
720720
return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));

‎compiler/rustc_hir_analysis/src/coherence/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed>
165165
let mut res = tcx.ensure_ok().specialization_graph_of(def_id);
166166

167167
for &impl_def_id in impls {
168-
let impl_header = tcx.impl_trait_header(impl_def_id).unwrap();
168+
let impl_header = tcx.impl_trait_header(impl_def_id);
169169
let trait_ref = impl_header.trait_ref.instantiate_identity();
170170
let trait_def = tcx.trait_def(trait_ref.def_id);
171171

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /