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

Consider types appearing in const expressions to be invariant #89829

Merged
merged 3 commits into from
Oct 23, 2021
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
30 changes: 25 additions & 5 deletions compiler/rustc_typeck/src/variance/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
self.add_constraints_from_region(current, lt, variance_i)
}
GenericArgKind::Type(ty) => self.add_constraints_from_ty(current, ty, variance_i),
GenericArgKind::Const(_) => {
// Consts impose no constraints.
GenericArgKind::Const(val) => {
self.add_constraints_from_const(current, val, variance_i)
}
}
}
Expand Down Expand Up @@ -263,7 +263,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
}

ty::Array(typ, _) => {
ty::Array(typ, len) => {
self.add_constraints_from_const(current, len, variance);
self.add_constraints_from_ty(current, typ, variance);
}

Expand Down Expand Up @@ -385,13 +386,32 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
self.add_constraints_from_region(current, lt, variance_i)
}
GenericArgKind::Type(ty) => self.add_constraints_from_ty(current, ty, variance_i),
GenericArgKind::Const(_) => {
// Consts impose no constraints.
GenericArgKind::Const(val) => {
self.add_constraints_from_const(current, val, variance)
}
}
}
}

/// Adds constraints appropriate for a const expression `val`
/// in a context with ambient variance `variance`
fn add_constraints_from_const(
&mut self,
current: &CurrentItem,
val: &ty::Const<'tcx>,
variance: VarianceTermPtr<'a>,
) {
debug!("add_constraints_from_const(val={:?}, variance={:?})", val, variance);

match &val.val {
ty::ConstKind::Unevaluated(uv) => {
let substs = uv.substs(self.tcx());
self.add_constraints_from_invariant_substs(current, substs, variance);
}
_ => {}
}
}

/// Adds constraints appropriate for a function with signature
/// `sig` appearing in a context with ambient variance `variance`
fn add_constraints_from_sig(
Expand Down
12 changes: 1 addition & 11 deletions src/test/ui/const-generics/issues/issue-67375.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,5 @@ LL | inner: [(); { [|_: &T| {}; 0].len() }],
|
= help: consider moving this anonymous constant into a `const` function

error[E0392]: parameter `T` is never used
--> $DIR/issue-67375.rs:5:12
|
LL | struct Bug<T> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `T` to be a const parameter, use `const T: usize` instead

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0392`.
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/issues/issue-67375.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![cfg_attr(full, feature(generic_const_exprs))]

struct Bug<T> {
//~^ ERROR parameter `T` is never used
//[min]~^ ERROR parameter `T` is never used
inner: [(); { [|_: &T| {}; 0].len() }],
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR overly complex generic constant
Expand Down
14 changes: 2 additions & 12 deletions src/test/ui/const-generics/issues/issue-67945-1.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ LL | let x: S = MaybeUninit::uninit();
= note: expected type parameter `S`
found union `MaybeUninit<_>`

error[E0392]: parameter `S` is never used
--> $DIR/issue-67945-1.rs:7:12
|
LL | struct Bug<S> {
| ^ unused parameter
|
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `S` to be a const parameter, use `const S: usize` instead

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors have detailed explanations: E0308, E0392.
For more information about an error, try `rustc --explain E0308`.
For more information about this error, try `rustc --explain E0308`.
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/issues/issue-67945-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::mem::MaybeUninit;

struct Bug<S> {
//~^ ERROR parameter `S` is never used
//[min]~^ ERROR parameter `S` is never used
A: [(); {
let x: S = MaybeUninit::uninit();
//[min]~^ ERROR generic parameters may not be used in const operations
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/variance/variance-associated-consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Test that the variance computation considers types that
// appear in const expressions to be invariant.

#![feature(rustc_attrs)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

trait Trait {
const Const: usize;
}

#[rustc_variance]
struct Foo<T: Trait> { //~ ERROR [o]
field: [u8; <T as Trait>::Const]
}

fn main() { }
10 changes: 10 additions & 0 deletions src/test/ui/variance/variance-associated-consts.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error[E0208]: [o]
--> $DIR/variance-associated-consts.rs:13:1
|
LL | / struct Foo<T: Trait> {
LL | | field: [u8; <T as Trait>::Const]
LL | | }
| |_^

error: aborting due to previous error