Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix oversight in the min_const_generics checks #93802

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2281,8 +2281,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
assert_eq!(opt_self_ty, None);
self.prohibit_generics(path.segments);
// Try to evaluate any array length constants.
let normalized_ty = self.normalize_ty(span, tcx.at(span).type_of(def_id));
if forbid_generic && normalized_ty.needs_subst() {
let ty = tcx.at(span).type_of(def_id);
lcnr marked this conversation as resolved.
Show resolved Hide resolved
// HACK(min_const_generics): Forbid generic `Self` types
// here as we can't easily do that during nameres.
//
// We do this before normalization as we otherwise allow
// ```rust
// trait AlwaysApplicable { type Assoc; }
// impl<T: ?Sized> AlwaysApplicable for T { type Assoc = usize; }
//
// trait BindsParam<T> {
// type ArrayTy;
// }
// impl<T> BindsParam<T> for <T as AlwaysApplicable>::Assoc {
// type ArrayTy = [u8; Self::MAX];
// }
// ```
// Note that the normalization happens in the param env of
// the anon const, which is empty. This is why the
// `AlwaysApplicable` impl needs a `T: ?Sized` bound for
// this to compile if we were to normalize here.
if forbid_generic && ty.needs_subst() {
let mut err = tcx.sess.struct_span_err(
path.span,
"generic `Self` types are currently not permitted in anonymous constants",
Expand All @@ -2297,7 +2316,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
err.emit();
tcx.ty_error()
} else {
normalized_ty
self.normalize_ty(span, ty)
}
}
Res::Def(DefKind::AssocTy, def_id) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait AlwaysApplicable {
type Assoc;
}
impl<T: ?Sized> AlwaysApplicable for T {
type Assoc = usize;
}

trait BindsParam<T> {
type ArrayTy;
}
impl<T> BindsParam<T> for <T as AlwaysApplicable>::Assoc {
type ArrayTy = [u8; Self::MAX]; //~ ERROR generic `Self` types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: generic `Self` types are currently not permitted in anonymous constants
--> $DIR/forbid-self-no-normalize.rs:12:25
|
LL | type ArrayTy = [u8; Self::MAX];
| ^^^^
|
note: not a concrete type
--> $DIR/forbid-self-no-normalize.rs:11:27
|
LL | impl<T> BindsParam<T> for <T as AlwaysApplicable>::Assoc {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error