Skip to content

Commit bd1c55a

Browse files
committed
fixes for ICEs and unexpected errors
1 parent d42b128 commit bd1c55a

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

compiler/rustc_hir_analysis/src/bounds.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use rustc_hir::{def::DefKind, LangItem};
55
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
6+
use rustc_span::sym;
67
use rustc_span::{def_id::DefId, Span};
78

89
/// Collects together a list of type bounds. These lists of bounds occur in many places
@@ -62,17 +63,34 @@ impl<'tcx> Bounds<'tcx> {
6263
Some(tcx.expected_host_effect_param_for_body(defining_def_id))
6364
}
6465

65-
(_, ty::BoundConstness::NotConst) => None,
66+
(_, ty::BoundConstness::NotConst) => {
67+
tcx.has_attr(trait_ref.def_id(), sym::const_trait).then_some(tcx.consts.true_)
68+
}
6669

6770
// if the defining_def_id is a trait, we wire it differently than others by equating the effects.
6871
(
69-
kind @ (DefKind::Trait | DefKind::Impl { of_trait: true }),
72+
kind @ (DefKind::Trait | DefKind::Impl { of_trait: true } | DefKind::AssocTy),
7073
ty::BoundConstness::ConstIfConst,
7174
) => {
72-
let trait_we_are_in = if let DefKind::Trait = kind {
73-
ty::TraitRef::identity(tcx, defining_def_id)
75+
let parent_def_id = if kind == DefKind::AssocTy {
76+
let did = tcx.parent(defining_def_id);
77+
if !matches!(
78+
tcx.def_kind(did),
79+
DefKind::Trait | DefKind::Impl { of_trait: true }
80+
) {
81+
tcx.dcx().span_delayed_bug(span, "invalid `~const` encountered");
82+
return;
83+
}
84+
did
7485
} else {
75-
tcx.impl_trait_ref(defining_def_id).unwrap().instantiate_identity()
86+
defining_def_id
87+
};
88+
let trait_we_are_in = match tcx.def_kind(parent_def_id) {
89+
DefKind::Trait => ty::TraitRef::identity(tcx, parent_def_id),
90+
DefKind::Impl { of_trait: true } => {
91+
tcx.impl_trait_ref(parent_def_id).unwrap().instantiate_identity()
92+
}
93+
_ => unreachable!(),
7694
};
7795
// create a new projection type `<T as TraitForBound>::Effects`
7896
let Some(assoc) = tcx.associated_type_for_effects(trait_ref.def_id()) else {

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ pub(super) fn check_specialization_validity<'tcx>(
733733
let result = opt_result.unwrap_or(Ok(()));
734734

735735
if let Err(parent_impl) = result {
736-
if !tcx.is_impl_trait_in_trait(impl_item) {
736+
if !tcx.is_impl_trait_in_trait(impl_item) && !tcx.is_effects_desugared_assoc_ty(impl_item) {
737737
report_forbidden_specialization(tcx, impl_item, parent_impl);
738738
} else {
739739
tcx.dcx().delayed_bug(format!("parent item: {parent_impl:?} not marked as default"));

0 commit comments

Comments
 (0)