Skip to content

Commit 27836e1

Browse files
Rigidly project missing item due to guaranteed impossible sized predicate
1 parent 48f89e7 commit 27836e1

File tree

8 files changed

+281
-52
lines changed

8 files changed

+281
-52
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ fn check_impl_items_against_trait<'tcx>(
943943
let cause = ObligationCause::misc(tcx.def_span(impl_id), impl_id);
944944
let param_env = tcx.param_env(impl_id);
945945

946-
let self_is_guaranteed_unsized = match tcx
946+
let self_is_guaranteed_unsized = tcx
947947
.struct_tail_raw(
948948
trait_ref.self_ty(),
949949
|ty| {
@@ -957,11 +957,7 @@ fn check_impl_items_against_trait<'tcx>(
957957
},
958958
|| (),
959959
)
960-
.kind()
961-
{
962-
ty::Dynamic(_, _, ty::DynKind::Dyn) | ty::Slice(_) | ty::Str => true,
963-
_ => false,
964-
};
960+
.is_guaranteed_unsized_raw();
965961

966962
for &impl_item in impl_item_refs {
967963
let ty_impl_item = tcx.associated_item(impl_item);

Diff for: compiler/rustc_middle/src/ty/sty.rs

+37
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,43 @@ impl<'tcx> Ty<'tcx> {
20292029
pub fn is_known_rigid(self) -> bool {
20302030
self.kind().is_known_rigid()
20312031
}
2032+
2033+
/// Returns true if the type is guaranteed to be one of the three built-in unsized types:
2034+
/// `dyn Trait`/`[T]`/`str`. This function is *raw* because it does not compute the struct
2035+
/// tail of the type, so you are responsible for doing that yourself.
2036+
// NOTE: Keep this in sync with `rustc_type_ir`'s copy.
2037+
pub fn is_guaranteed_unsized_raw(self) -> bool {
2038+
match self.kind() {
2039+
Dynamic(_, _, ty::Dyn) | ty::Slice(_) | ty::Str => true,
2040+
Bool
2041+
| Char
2042+
| Int(_)
2043+
| Uint(_)
2044+
| Float(_)
2045+
| Adt(_, _)
2046+
| Foreign(_)
2047+
| Array(_, _)
2048+
| Pat(_, _)
2049+
| RawPtr(_, _)
2050+
| Ref(_, _, _)
2051+
| FnDef(_, _)
2052+
| FnPtr(_, _)
2053+
| UnsafeBinder(_)
2054+
| Closure(_, _)
2055+
| CoroutineClosure(_, _)
2056+
| Coroutine(_, _)
2057+
| CoroutineWitness(_, _)
2058+
| Never
2059+
| Tuple(_)
2060+
| Alias(_, _)
2061+
| Param(_)
2062+
| Bound(_, _)
2063+
| Placeholder(_)
2064+
| Infer(_)
2065+
| Error(_)
2066+
| Dynamic(_, _, ty::DynStar) => false,
2067+
}
2068+
}
20322069
}
20332070

20342071
impl<'tcx> rustc_type_ir::inherent::Tys<TyCtxt<'tcx>> for &'tcx ty::List<Ty<'tcx>> {

Diff for: compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,17 @@ where
232232
};
233233

234234
if !cx.has_item_definition(target_item_def_id) {
235-
return error_response(ecx, cx.delay_bug("missing item"));
235+
// If the impl is missing an item, it's either because the user forgot to
236+
// provide it, or the user is not *obligated* to provide it (because it
237+
// has a trivially false `Sized` predicate). If it's the latter, we cannot
238+
// delay a bug because we can have trivially false where clauses, so we
239+
// treat it as rigid.
240+
if goal_trait_ref.self_ty().is_guaranteed_unsized_raw() {
241+
ecx.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
242+
return ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
243+
} else {
244+
return error_response(ecx, cx.delay_bug("missing item"));
245+
}
236246
}
237247

238248
let target_container_def_id = cx.parent(target_item_def_id);

Diff for: compiler/rustc_trait_selection/src/traits/project.rs

+59-45
Original file line numberDiff line numberDiff line change
@@ -669,30 +669,11 @@ fn project<'cx, 'tcx>(
669669

670670
match candidates {
671671
ProjectionCandidateSet::Single(candidate) => {
672-
Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate)))
672+
confirm_candidate(selcx, obligation, candidate)
673673
}
674674
ProjectionCandidateSet::None => {
675675
let tcx = selcx.tcx();
676-
let term = match tcx.def_kind(obligation.predicate.def_id) {
677-
DefKind::AssocTy => Ty::new_projection_from_args(
678-
tcx,
679-
obligation.predicate.def_id,
680-
obligation.predicate.args,
681-
)
682-
.into(),
683-
DefKind::AssocConst => ty::Const::new_unevaluated(
684-
tcx,
685-
ty::UnevaluatedConst::new(
686-
obligation.predicate.def_id,
687-
obligation.predicate.args,
688-
),
689-
)
690-
.into(),
691-
kind => {
692-
bug!("unknown projection def-id: {}", kind.descr(obligation.predicate.def_id))
693-
}
694-
};
695-
676+
let term = obligation.predicate.to_term(tcx);
696677
Ok(Projected::NoProgress(term))
697678
}
698679
// Error occurred while trying to processing impls.
@@ -1244,18 +1225,16 @@ fn confirm_candidate<'cx, 'tcx>(
12441225
selcx: &mut SelectionContext<'cx, 'tcx>,
12451226
obligation: &ProjectionTermObligation<'tcx>,
12461227
candidate: ProjectionCandidate<'tcx>,
1247-
) -> Progress<'tcx> {
1228+
) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
12481229
debug!(?obligation, ?candidate, "confirm_candidate");
1249-
let mut progress = match candidate {
1230+
let mut result = match candidate {
12501231
ProjectionCandidate::ParamEnv(poly_projection)
1251-
| ProjectionCandidate::Object(poly_projection) => {
1252-
confirm_param_env_candidate(selcx, obligation, poly_projection, false)
1253-
}
1254-
1255-
ProjectionCandidate::TraitDef(poly_projection) => {
1256-
confirm_param_env_candidate(selcx, obligation, poly_projection, true)
1257-
}
1258-
1232+
| ProjectionCandidate::Object(poly_projection) => Ok(Projected::Progress(
1233+
confirm_param_env_candidate(selcx, obligation, poly_projection, false),
1234+
)),
1235+
ProjectionCandidate::TraitDef(poly_projection) => Ok(Projected::Progress(
1236+
confirm_param_env_candidate(selcx, obligation, poly_projection, true),
1237+
)),
12591238
ProjectionCandidate::Select(impl_source) => {
12601239
confirm_select_candidate(selcx, obligation, impl_source)
12611240
}
@@ -1266,23 +1245,26 @@ fn confirm_candidate<'cx, 'tcx>(
12661245
// with new region variables, we need to resolve them to existing variables
12671246
// when possible for this to work. See `auto-trait-projection-recursion.rs`
12681247
// for a case where this matters.
1269-
if progress.term.has_infer_regions() {
1248+
if let Ok(Projected::Progress(progress)) = &mut result
1249+
&& progress.term.has_infer_regions()
1250+
{
12701251
progress.term = progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx));
12711252
}
1272-
progress
1253+
1254+
result
12731255
}
12741256

12751257
fn confirm_select_candidate<'cx, 'tcx>(
12761258
selcx: &mut SelectionContext<'cx, 'tcx>,
12771259
obligation: &ProjectionTermObligation<'tcx>,
12781260
impl_source: Selection<'tcx>,
1279-
) -> Progress<'tcx> {
1261+
) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
12801262
match impl_source {
12811263
ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
12821264
ImplSource::Builtin(BuiltinImplSource::Misc | BuiltinImplSource::Trivial, data) => {
12831265
let tcx = selcx.tcx();
12841266
let trait_def_id = obligation.predicate.trait_def_id(tcx);
1285-
if tcx.is_lang_item(trait_def_id, LangItem::Coroutine) {
1267+
let progress = if tcx.is_lang_item(trait_def_id, LangItem::Coroutine) {
12861268
confirm_coroutine_candidate(selcx, obligation, data)
12871269
} else if tcx.is_lang_item(trait_def_id, LangItem::Future) {
12881270
confirm_future_candidate(selcx, obligation, data)
@@ -1304,7 +1286,8 @@ fn confirm_select_candidate<'cx, 'tcx>(
13041286
confirm_async_fn_kind_helper_candidate(selcx, obligation, data)
13051287
} else {
13061288
confirm_builtin_candidate(selcx, obligation, data)
1307-
}
1289+
};
1290+
Ok(Projected::Progress(progress))
13081291
}
13091292
ImplSource::Builtin(BuiltinImplSource::Object { .. }, _)
13101293
| ImplSource::Param(..)
@@ -2000,7 +1983,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20001983
selcx: &mut SelectionContext<'cx, 'tcx>,
20011984
obligation: &ProjectionTermObligation<'tcx>,
20021985
impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
2003-
) -> Progress<'tcx> {
1986+
) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
20041987
let tcx = selcx.tcx();
20051988

20061989
let ImplSourceUserDefinedData { impl_def_id, args, mut nested } = impl_impl_source;
@@ -2011,19 +1994,47 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20111994
let param_env = obligation.param_env;
20121995
let assoc_ty = match specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) {
20131996
Ok(assoc_ty) => assoc_ty,
2014-
Err(guar) => return Progress::error(tcx, guar),
1997+
Err(guar) => return Ok(Projected::Progress(Progress::error(tcx, guar))),
20151998
};
1999+
2000+
// This means that the impl is missing a definition for the
2001+
// associated type. This is either because the associate item
2002+
// has impossible-to-satisfy predicates (since those were
2003+
// allowed in <https://github.com/rust-lang/rust/pull/135480>),
2004+
// or because the impl is literally missing the definition.
20162005
if !assoc_ty.item.defaultness(tcx).has_value() {
2017-
// This means that the impl is missing a definition for the
2018-
// associated type. This error will be reported by the type
2019-
// checker method `check_impl_items_against_trait`, so here we
2020-
// just return Error.
20212006
debug!(
20222007
"confirm_impl_candidate: no associated type {:?} for {:?}",
20232008
assoc_ty.item.name, obligation.predicate
20242009
);
2025-
return Progress { term: Ty::new_misc_error(tcx).into(), obligations: nested };
2010+
let tail = selcx.tcx().struct_tail_raw(
2011+
tcx.type_of(impl_def_id).instantiate(tcx, args),
2012+
|ty| {
2013+
normalize_with_depth_to(
2014+
selcx,
2015+
obligation.param_env,
2016+
obligation.cause.clone(),
2017+
obligation.recursion_depth + 1,
2018+
ty,
2019+
&mut nested,
2020+
)
2021+
},
2022+
|| {},
2023+
);
2024+
if tail.is_guaranteed_unsized_raw() {
2025+
// We treat this projection as rigid here, which is represented via
2026+
// `Projected::NoProgress`. This will ensure that the projection is
2027+
// checked for well-formedness, and it's either satisfied by a trivial
2028+
// where clause in its env or it results in an error.
2029+
return Ok(Projected::NoProgress(obligation.predicate.to_term(tcx)));
2030+
} else {
2031+
return Ok(Projected::Progress(Progress {
2032+
term: Ty::new_misc_error(tcx).into(),
2033+
obligations: nested,
2034+
}));
2035+
}
20262036
}
2037+
20272038
// If we're trying to normalize `<Vec<u32> as X>::A<S>` using
20282039
//`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
20292040
//
@@ -2033,6 +2044,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20332044
let args = obligation.predicate.args.rebase_onto(tcx, trait_def_id, args);
20342045
let args = translate_args(selcx.infcx, param_env, impl_def_id, args, assoc_ty.defining_node);
20352046
let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2047+
20362048
let term: ty::EarlyBinder<'tcx, ty::Term<'tcx>> = if is_const {
20372049
let did = assoc_ty.item.def_id;
20382050
let identity_args = crate::traits::GenericArgs::identity_for_item(tcx, did);
@@ -2041,7 +2053,8 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20412053
} else {
20422054
tcx.type_of(assoc_ty.item.def_id).map_bound(|ty| ty.into())
20432055
};
2044-
if !tcx.check_args_compatible(assoc_ty.item.def_id, args) {
2056+
2057+
let progress = if !tcx.check_args_compatible(assoc_ty.item.def_id, args) {
20452058
let err = Ty::new_error_with_message(
20462059
tcx,
20472060
obligation.cause.span,
@@ -2051,7 +2064,8 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20512064
} else {
20522065
assoc_ty_own_obligations(selcx, obligation, &mut nested);
20532066
Progress { term: term.instantiate(tcx, args), obligations: nested }
2054-
}
2067+
};
2068+
Ok(Projected::Progress(progress))
20552069
}
20562070

20572071
// Get obligations corresponding to the predicates from the where-clause of the

Diff for: compiler/rustc_type_ir/src/inherent.rs

+33
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ pub trait Ty<I: Interner<Ty = Self>>:
155155
fn is_known_rigid(self) -> bool {
156156
self.kind().is_known_rigid()
157157
}
158+
159+
fn is_guaranteed_unsized_raw(self) -> bool {
160+
match self.kind() {
161+
ty::Dynamic(_, _, ty::Dyn) | ty::Slice(_) | ty::Str => true,
162+
ty::Bool
163+
| ty::Char
164+
| ty::Int(_)
165+
| ty::Uint(_)
166+
| ty::Float(_)
167+
| ty::Adt(_, _)
168+
| ty::Foreign(_)
169+
| ty::Array(_, _)
170+
| ty::Pat(_, _)
171+
| ty::RawPtr(_, _)
172+
| ty::Ref(_, _, _)
173+
| ty::FnDef(_, _)
174+
| ty::FnPtr(_, _)
175+
| ty::UnsafeBinder(_)
176+
| ty::Closure(_, _)
177+
| ty::CoroutineClosure(_, _)
178+
| ty::Coroutine(_, _)
179+
| ty::CoroutineWitness(_, _)
180+
| ty::Never
181+
| ty::Tuple(_)
182+
| ty::Alias(_, _)
183+
| ty::Param(_)
184+
| ty::Bound(_, _)
185+
| ty::Placeholder(_)
186+
| ty::Infer(_)
187+
| ty::Error(_)
188+
| ty::Dynamic(_, _, ty::DynStar) => false,
189+
}
190+
}
158191
}
159192

160193
pub trait Tys<I: Interner<Tys = Self>>:
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0277]: the size for values of type `[()]` cannot be known at compilation time
2+
--> $DIR/trivial-unsized-projection.rs:20:12
3+
|
4+
LL | const FOO: <[()] as Bad>::Assert = todo!();
5+
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[()]`
8+
note: required by a bound in `Bad::Assert`
9+
--> $DIR/trivial-unsized-projection.rs:14:15
10+
|
11+
LL | type Assert
12+
| ------ required by a bound in this associated type
13+
LL | where
14+
LL | Self: Sized;
15+
| ^^^^^ required by this bound in `Bad::Assert`
16+
help: consider relaxing the implicit `Sized` restriction
17+
|
18+
LL | type Assert: ?Sized
19+
| ++++++++
20+
21+
error[E0277]: the size for values of type `[()]` cannot be known at compilation time
22+
--> $DIR/trivial-unsized-projection.rs:20:12
23+
|
24+
LL | const FOO: <[()] as Bad>::Assert = todo!();
25+
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
26+
|
27+
= help: the trait `Sized` is not implemented for `[()]`
28+
note: required by a bound in `Bad::Assert`
29+
--> $DIR/trivial-unsized-projection.rs:14:15
30+
|
31+
LL | type Assert
32+
| ------ required by a bound in this associated type
33+
LL | where
34+
LL | Self: Sized;
35+
| ^^^^^ required by this bound in `Bad::Assert`
36+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
37+
help: consider relaxing the implicit `Sized` restriction
38+
|
39+
LL | type Assert: ?Sized
40+
| ++++++++
41+
42+
error: aborting due to 2 previous errors
43+
44+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)