Skip to content

Commit

Permalink
don't leave assoc const unnormalized due to unconstrained params
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Feb 21, 2025
1 parent a825e37 commit 7fea935
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use rustc_errors::{Applicability, Diag, EmissionGuarantee};
use rustc_errors::{Applicability, Diag, EmissionGuarantee, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -996,4 +996,7 @@ pub enum CodegenObligationError {
/// but was included during typeck due to the trivial_bounds feature.
Unimplemented,
FulfillmentError,
/// The selected impl has unconstrained generic parameters. This will emit an error
/// during impl WF checking.
UnconstrainedParam(ErrorGuaranteed),
}
16 changes: 7 additions & 9 deletions compiler/rustc_traits/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,14 @@ pub(crate) fn codegen_select_candidate<'tcx>(
// but never resolved, causing the return value of a query to contain inference
// vars. We do not have a concept for this and will in fact ICE in stable hashing
// of the return value. So bail out instead.
match impl_source {
ImplSource::UserDefined(impl_) => {
tcx.dcx().span_delayed_bug(
tcx.def_span(impl_.impl_def_id),
"this impl has unconstrained generic parameters",
);
}
let guar = match impl_source {
ImplSource::UserDefined(impl_) => tcx.dcx().span_delayed_bug(
tcx.def_span(impl_.impl_def_id),
"this impl has unconstrained generic parameters",
),
_ => unreachable!(),
}
return Err(CodegenObligationError::FulfillmentError);
};
return Err(CodegenObligationError::UnconstrainedParam(guar));
}

Ok(&*tcx.arena.alloc(impl_source))
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn resolve_associated_item<'tcx>(
| CodegenObligationError::Unimplemented
| CodegenObligationError::FulfillmentError,
) => return Ok(None),
Err(CodegenObligationError::UnconstrainedParam(guar)) => return Err(guar),
};

// Now that we know which impl is being used, we can dispatch to
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/layout/unconstrained-param-ice-137308.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ impl<C: ?Sized> A for u8 { //~ ERROR: the type parameter `C` is not constrained
}

#[rustc_layout(debug)]
struct S([u8; <u8 as A>::B]); //~ ERROR: the type `[u8; <u8 as A>::B]` has an unknown layout
struct S([u8; <u8 as A>::B]); //~ ERROR: the type has an unknown layout
2 changes: 1 addition & 1 deletion tests/ui/layout/unconstrained-param-ice-137308.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0207]: the type parameter `C` is not constrained by the impl trait, self
LL | impl<C: ?Sized> A for u8 {
| ^ unconstrained type parameter

error: the type `[u8; <u8 as A>::B]` has an unknown layout
error: the type has an unknown layout
--> $DIR/unconstrained-param-ice-137308.rs:18:1
|
LL | struct S([u8; <u8 as A>::B]);
Expand Down

0 comments on commit 7fea935

Please sign in to comment.