Skip to content

Commit 50fb40a

Browse files
Delay a bug when encountering an impl with unconstrained generics in codegen_select
1 parent f5be3ca commit 50fb40a

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

compiler/rustc_traits/src/codegen.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,21 @@ pub(crate) fn codegen_select_candidate<'tcx>(
7474
}
7575

7676
let impl_source = infcx.resolve_vars_if_possible(impl_source);
77-
let impl_source = infcx.tcx.erase_regions(impl_source);
78-
if impl_source.has_infer() {
79-
// Unused lifetimes on an impl get replaced with inference vars, but never resolved,
80-
// causing the return value of a query to contain inference vars. We do not have a concept
81-
// for this and will in fact ICE in stable hashing of the return value. So bail out instead.
82-
infcx.tcx.dcx().has_errors().unwrap();
77+
let impl_source = tcx.erase_regions(impl_source);
78+
if impl_source.has_non_region_infer() {
79+
// Unused generic types or consts on an impl get replaced with inference vars,
80+
// but never resolved, causing the return value of a query to contain inference
81+
// vars. We do not have a concept for this and will in fact ICE in stable hashing
82+
// of the return value. So bail out instead.
83+
match impl_source {
84+
ImplSource::UserDefined(impl_) => {
85+
tcx.dcx().span_delayed_bug(
86+
tcx.def_span(impl_.impl_def_id),
87+
"this impl has unconstrained generic parameters",
88+
);
89+
}
90+
_ => unreachable!(),
91+
}
8392
return Err(CodegenObligationError::FulfillmentError);
8493
}
8594

tests/crashes/126646.rs

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Need a different module so we try to build the mir for `test`
2+
// before analyzing `mod foo`.
3+
4+
mod foo {
5+
pub trait Callable {
6+
fn call();
7+
}
8+
9+
impl<V: ?Sized> Callable for () {
10+
//~^ ERROR the type parameter `V` is not constrained by the impl trait, self type, or predicates
11+
fn call() {}
12+
}
13+
}
14+
use foo::*;
15+
16+
fn test() -> impl Sized {
17+
<() as Callable>::call()
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/resolve-impl-before-constrain-check.rs:9:10
3+
|
4+
LL | impl<V: ?Sized> Callable for () {
5+
| ^ unconstrained type parameter
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)