Skip to content

Commit 76c562f

Browse files
committed
fix min_const_generics oversight
1 parent 775e480 commit 76c562f

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -2281,8 +2281,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22812281
assert_eq!(opt_self_ty, None);
22822282
self.prohibit_generics(path.segments);
22832283
// Try to evaluate any array length constants.
2284-
let normalized_ty = self.normalize_ty(span, tcx.at(span).type_of(def_id));
2285-
if forbid_generic && normalized_ty.needs_subst() {
2284+
let ty = tcx.at(span).type_of(def_id);
2285+
// HACK(min_const_generics): Forbid generic `Self` types
2286+
// here as we can't easily do that during nameres.
2287+
//
2288+
// We do this before normalization as we otherwise allow
2289+
// ```rust
2290+
// trait AlwaysApplicable { type Assoc; }
2291+
// impl<T: ?Sized> AlwaysApplicable for T { type Assoc = usize; }
2292+
//
2293+
// trait BindsParam<T> {
2294+
// type ArrayTy;
2295+
// }
2296+
// impl<T> BindsParam<T> for <T as AlwaysApplicable>::Assoc {
2297+
// type ArrayTy = [u8; Self::MAX];
2298+
// }
2299+
// ```
2300+
// Note that the normalization happens in the param env of
2301+
// the anon const, which is empty. This is why the
2302+
// `AlwaysApplicable` impl needs a `T: ?Sized` bound for
2303+
// this to compile if we were to normalize here.
2304+
if forbid_generic && ty.needs_subst() {
22862305
let mut err = tcx.sess.struct_span_err(
22872306
path.span,
22882307
"generic `Self` types are currently not permitted in anonymous constants",
@@ -2297,7 +2316,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22972316
err.emit();
22982317
tcx.ty_error()
22992318
} else {
2300-
normalized_ty
2319+
self.normalize_ty(span, ty)
23012320
}
23022321
}
23032322
Res::Def(DefKind::AssocTy, def_id) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait AlwaysApplicable {
2+
type Assoc;
3+
}
4+
impl<T: ?Sized> AlwaysApplicable for T {
5+
type Assoc = usize;
6+
}
7+
8+
trait BindsParam<T> {
9+
type ArrayTy;
10+
}
11+
impl<T> BindsParam<T> for <T as AlwaysApplicable>::Assoc {
12+
type ArrayTy = [u8; Self::MAX]; //~ ERROR generic `Self` types
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: generic `Self` types are currently not permitted in anonymous constants
2+
--> $DIR/forbid-self-no-normalize.rs:12:25
3+
|
4+
LL | type ArrayTy = [u8; Self::MAX];
5+
| ^^^^
6+
|
7+
note: not a concrete type
8+
--> $DIR/forbid-self-no-normalize.rs:11:27
9+
|
10+
LL | impl<T> BindsParam<T> for <T as AlwaysApplicable>::Assoc {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)