about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-12-05 00:37:58 +0100
committerGitHub <noreply@github.com>2021-12-05 00:37:58 +0100
commit1f2a26e999a4c8e3a053e95f62b1ee403b15faed (patch)
tree94c877d9923066b9f7e615547c4a4c7e529757e1
parent29fe57def2693a3f9b4fcfbb1072b4b655700260 (diff)
parent37ed2db1e04e5b9dcdf833374d364b1f5e374fc3 (diff)
downloadrust-1f2a26e999a4c8e3a053e95f62b1ee403b15faed.tar.gz
rust-1f2a26e999a4c8e3a053e95f62b1ee403b15faed.zip
Rollup merge of #90023 - b-naber:postpone_const_eval_infer_vars, r=nikomatsakis
Postpone the evaluation of constant expressions that depend on inference variables

Previously `delay_span_bug` calls were triggered once an inference variable was included in the substs of a constant that was to be evaluated. Some of these would merely have resulted in trait candidates being rejected, hence no real error was ever encountered, but the triggering of the `delay_span_bug` then caused an ICE in later stages of the compiler due to no error ever occurring.
We now postpone the evaluation of these constants, so any trait obligation fulfillment will simply stall on this constant and the existing type inference machinery of the compiler handles any type errors if present.

Fixes https://github.com/rust-lang/rust/issues/89320
Fixes https://github.com/rust-lang/rust/issues/89146
Fixes https://github.com/rust-lang/rust/issues/87964
Fixes https://github.com/rust-lang/rust/issues/87470
Fixes https://github.com/rust-lang/rust/issues/83288
Fixes https://github.com/rust-lang/rust/issues/83249
Fixes https://github.com/rust-lang/rust/issues/90654

I want to thank `@BoxyUwU` for cooperating on this and for providing some help.

r? `@lcnr` maybe?
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs92
-rw-r--r--compiler/rustc_infer/src/infer/mod.rs23
-rw-r--r--src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs8
-rw-r--r--src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.stderr33
-rw-r--r--src/test/ui/const-generics/issues/issue-83249.rs23
-rw-r--r--src/test/ui/const-generics/issues/issue-83249.stderr11
-rw-r--r--src/test/ui/const-generics/issues/issue-83288.rs69
-rw-r--r--src/test/ui/const-generics/issues/issue-87470.rs24
-rw-r--r--src/test/ui/const-generics/issues/issue-87964.rs29
-rw-r--r--src/test/ui/const-generics/issues/issue-89146.rs26
-rw-r--r--src/test/ui/const-generics/issues/issue-89320.rs19
11 files changed, 324 insertions, 33 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
index a7e019a53ee..32c02033dc9 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
@@ -1,5 +1,6 @@
 use crate::infer::type_variable::TypeVariableOriginKind;
 use crate::infer::InferCtxt;
+use crate::rustc_middle::ty::TypeFoldable;
 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
 use rustc_hir as hir;
 use rustc_hir::def::{DefKind, Namespace};
@@ -400,36 +401,75 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                 }
             }
             GenericArgKind::Const(ct) => {
-                if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
-                    let origin =
-                        self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
-                    if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
-                        origin.kind
-                    {
-                        return InferenceDiagnosticsData {
-                            name: name.to_string(),
+                match ct.val {
+                    ty::ConstKind::Infer(InferConst::Var(vid)) => {
+                        let origin = self
+                            .inner
+                            .borrow_mut()
+                            .const_unification_table()
+                            .probe_value(vid)
+                            .origin;
+                        if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
+                            origin.kind
+                        {
+                            return InferenceDiagnosticsData {
+                                name: name.to_string(),
+                                span: Some(origin.span),
+                                kind: UnderspecifiedArgKind::Const { is_parameter: true },
+                                parent: InferenceDiagnosticsParentData::for_def_id(
+                                    self.tcx, def_id,
+                                ),
+                            };
+                        }
+
+                        debug_assert!(!origin.span.is_dummy());
+                        let mut s = String::new();
+                        let mut printer =
+                            ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::ValueNS);
+                        if let Some(highlight) = highlight {
+                            printer.region_highlight_mode = highlight;
+                        }
+                        let _ = ct.print(printer);
+                        InferenceDiagnosticsData {
+                            name: s,
                             span: Some(origin.span),
-                            kind: UnderspecifiedArgKind::Const { is_parameter: true },
-                            parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id),
-                        };
+                            kind: UnderspecifiedArgKind::Const { is_parameter: false },
+                            parent: None,
+                        }
                     }
-
-                    debug_assert!(!origin.span.is_dummy());
-                    let mut s = String::new();
-                    let mut printer =
-                        ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::ValueNS);
-                    if let Some(highlight) = highlight {
-                        printer.region_highlight_mode = highlight;
+                    ty::ConstKind::Unevaluated(ty::Unevaluated {
+                        substs_: Some(substs), ..
+                    }) => {
+                        assert!(substs.has_infer_types_or_consts());
+
+                        // FIXME: We only use the first inference variable we encounter in
+                        // `substs` here, this gives insufficiently informative diagnostics
+                        // in case there are multiple inference variables
+                        for s in substs.iter() {
+                            match s.unpack() {
+                                GenericArgKind::Type(t) => match t.kind() {
+                                    ty::Infer(_) => {
+                                        return self.extract_inference_diagnostics_data(s, None);
+                                    }
+                                    _ => {}
+                                },
+                                GenericArgKind::Const(c) => match c.val {
+                                    ty::ConstKind::Infer(InferConst::Var(_)) => {
+                                        return self.extract_inference_diagnostics_data(s, None);
+                                    }
+                                    _ => {}
+                                },
+                                _ => {}
+                            }
+                        }
+                        bug!(
+                            "expected an inference variable in substs of unevaluated const {:?}",
+                            ct
+                        );
                     }
-                    let _ = ct.print(printer);
-                    InferenceDiagnosticsData {
-                        name: s,
-                        span: Some(origin.span),
-                        kind: UnderspecifiedArgKind::Const { is_parameter: false },
-                        parent: None,
+                    _ => {
+                        bug!("unexpect const: {:?}", ct);
                     }
-                } else {
-                    bug!("unexpect const: {:?}", ct);
                 }
             }
             GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs
index 2fd01c2d595..48dfa0b6342 100644
--- a/compiler/rustc_infer/src/infer/mod.rs
+++ b/compiler/rustc_infer/src/infer/mod.rs
@@ -21,6 +21,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
 use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
 use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
+use rustc_middle::mir::interpret::ErrorHandled;
 use rustc_middle::mir::interpret::EvalToConstValueResult;
 use rustc_middle::traits::select;
 use rustc_middle::ty::error::{ExpectedFound, TypeError};
@@ -1584,13 +1585,27 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
         unevaluated: ty::Unevaluated<'tcx>,
         span: Option<Span>,
     ) -> EvalToConstValueResult<'tcx> {
-        let mut original_values = OriginalQueryValues::default();
-        let canonical = self.canonicalize_query((param_env, unevaluated), &mut original_values);
+        let mut substs = unevaluated.substs(self.tcx);
+        substs = self.resolve_vars_if_possible(substs);
+
+        // Postpone the evaluation of constants whose substs depend on inference
+        // variables
+        if substs.has_infer_types_or_consts() {
+            return Err(ErrorHandled::TooGeneric);
+        }
+
+        let param_env_erased = self.tcx.erase_regions(param_env);
+        let substs_erased = self.tcx.erase_regions(substs);
+
+        let unevaluated = ty::Unevaluated {
+            def: unevaluated.def,
+            substs_: Some(substs_erased),
+            promoted: unevaluated.promoted,
+        };
 
-        let (param_env, unevaluated) = canonical.value;
         // The return value is the evaluated value which doesn't contain any reference to inference
         // variables, thus we don't need to substitute back the original values.
-        self.tcx.const_eval_resolve(param_env, unevaluated, span)
+        self.tcx.const_eval_resolve(param_env_erased, unevaluated, span)
     }
 
     /// If `typ` is a type variable of some kind, resolve it one level
diff --git a/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs b/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs
index b79bc262d2b..18f33acaabb 100644
--- a/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs
+++ b/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs
@@ -1,4 +1,3 @@
-// run-pass
 #![feature(generic_const_exprs)]
 #![allow(incomplete_features)]
 
@@ -22,8 +21,11 @@ where
 }
 
 fn main() {
-    // Test that we can correctly infer `T` which requires evaluating
-    // `{ N + 1 }` which has substs containing an inference var
+    // FIXME(generic_const_exprs): We can't correctly infer `T` which requires
+    // evaluating `{ N + 1 }` which has substs containing an inference var
     let mut _q = Default::default();
+    //~^ ERROR type annotations needed
+
     _q = foo::<_, 2>(_q);
+    //~^ ERROR type annotations needed
 }
diff --git a/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.stderr b/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.stderr
new file mode 100644
index 00000000000..e59f1ac8027
--- /dev/null
+++ b/src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.stderr
@@ -0,0 +1,33 @@
+error[E0282]: type annotations needed
+  --> $DIR/const_eval_resolve_canonical.rs:26:9
+   |
+LL |     let mut _q = Default::default();
+   |         ^^^^^^ consider giving `_q` a type
+
+error[E0283]: type annotations needed
+  --> $DIR/const_eval_resolve_canonical.rs:29:10
+   |
+LL |     _q = foo::<_, 2>(_q);
+   |          ^^^^^^^^^^^ cannot infer type
+   |
+note: multiple `impl`s satisfying `(): Foo<{ N + 1 }>` found
+  --> $DIR/const_eval_resolve_canonical.rs:8:1
+   |
+LL | impl Foo<0> for () {
+   | ^^^^^^^^^^^^^^^^^^
+...
+LL | impl Foo<3> for () {
+   | ^^^^^^^^^^^^^^^^^^
+note: required by a bound in `foo`
+  --> $DIR/const_eval_resolve_canonical.rs:18:9
+   |
+LL | fn foo<T, const N: usize>(_: T) -> <() as Foo<{ N + 1 }>>::Assoc
+   |    --- required by a bound in this
+LL | where
+LL |     (): Foo<{ N + 1 }>,
+   |         ^^^^^^^^^^^^^^ required by this bound in `foo`
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0282, E0283.
+For more information about an error, try `rustc --explain E0282`.
diff --git a/src/test/ui/const-generics/issues/issue-83249.rs b/src/test/ui/const-generics/issues/issue-83249.rs
new file mode 100644
index 00000000000..65148c55ee5
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-83249.rs
@@ -0,0 +1,23 @@
+#![allow(incomplete_features)]
+#![feature(generic_const_exprs)]
+
+trait Foo {
+    const N: usize;
+}
+
+impl Foo for u8 {
+    const N: usize = 1;
+}
+
+fn foo<T: Foo>(_: [u8; T::N]) -> T {
+    todo!()
+}
+
+pub fn bar() {
+    let _: u8 = foo([0; 1]);
+
+    let _ = foo([0; 1]);
+    //~^ ERROR type annotations needed
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/issues/issue-83249.stderr b/src/test/ui/const-generics/issues/issue-83249.stderr
new file mode 100644
index 00000000000..402b3aa2d61
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-83249.stderr
@@ -0,0 +1,11 @@
+error[E0282]: type annotations needed
+  --> $DIR/issue-83249.rs:19:13
+   |
+LL |     let _ = foo([0; 1]);
+   |         -   ^^^ cannot infer type for type parameter `T` declared on the function `foo`
+   |         |
+   |         consider giving this pattern a type
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/const-generics/issues/issue-83288.rs b/src/test/ui/const-generics/issues/issue-83288.rs
new file mode 100644
index 00000000000..a24596d242e
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-83288.rs
@@ -0,0 +1,69 @@
+// build-pass
+
+#![allow(incomplete_features)]
+#![feature(generic_const_exprs)]
+
+use std::{marker::PhantomData, ops::Mul};
+
+pub enum Nil {}
+pub struct Cons<T, L> {
+    _phantom: PhantomData<(T, L)>,
+}
+
+pub trait Indices<const N: usize> {
+    const RANK: usize;
+    const NUM_ELEMS: usize;
+}
+
+impl<const N: usize> Indices<N> for Nil {
+    const RANK: usize = 0;
+    const NUM_ELEMS: usize = 1;
+}
+
+impl<T, I: Indices<N>, const N: usize> Indices<N> for Cons<T, I> {
+    const RANK: usize = I::RANK + 1;
+    const NUM_ELEMS: usize = I::NUM_ELEMS * N;
+}
+
+pub trait Concat<J> {
+    type Output;
+}
+
+impl<J> Concat<J> for Nil {
+    type Output = J;
+}
+
+impl<T, I, J> Concat<J> for Cons<T, I>
+where
+    I: Concat<J>,
+{
+    type Output = Cons<T, <I as Concat<J>>::Output>;
+}
+
+pub struct Tensor<I: Indices<N>, const N: usize>
+where
+    [u8; I::NUM_ELEMS]: Sized,
+{
+    pub data: [u8; I::NUM_ELEMS],
+    _phantom: PhantomData<I>,
+}
+
+impl<I: Indices<N>, J: Indices<N>, const N: usize> Mul<Tensor<J, N>> for Tensor<I, N>
+where
+    I: Concat<J>,
+    <I as Concat<J>>::Output: Indices<N>,
+    [u8; I::NUM_ELEMS]: Sized,
+    [u8; J::NUM_ELEMS]: Sized,
+    [u8; <I as Concat<J>>::Output::NUM_ELEMS]: Sized,
+{
+    type Output = Tensor<<I as Concat<J>>::Output, N>;
+
+    fn mul(self, _rhs: Tensor<J, N>) -> Self::Output {
+        Tensor {
+            data: [0u8; <I as Concat<J>>::Output::NUM_ELEMS],
+            _phantom: PhantomData,
+        }
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/issues/issue-87470.rs b/src/test/ui/const-generics/issues/issue-87470.rs
new file mode 100644
index 00000000000..d60181a418a
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-87470.rs
@@ -0,0 +1,24 @@
+// build-pass
+
+#![feature(generic_const_exprs)]
+#![allow(incomplete_features)]
+
+pub trait TraitWithConst {
+    const SOME_CONST: usize;
+}
+
+pub trait OtherTrait: TraitWithConst {
+    fn some_fn(self) -> [u8 ; <Self as TraitWithConst>::SOME_CONST];
+}
+
+impl TraitWithConst for f32 {
+    const SOME_CONST: usize = 32;
+}
+
+impl OtherTrait for f32 {
+    fn some_fn(self) -> [u8 ; <Self as TraitWithConst>::SOME_CONST] {
+        [0; 32]
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/issues/issue-87964.rs b/src/test/ui/const-generics/issues/issue-87964.rs
new file mode 100644
index 00000000000..116686abb9e
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-87964.rs
@@ -0,0 +1,29 @@
+// build-pass
+
+#![feature(generic_const_exprs)]
+#![allow(incomplete_features)]
+
+pub trait Target {
+    const LENGTH: usize;
+}
+
+
+pub struct Container<T: Target>
+where
+    [(); T::LENGTH]: Sized,
+{
+    _target: T,
+}
+
+impl<T: Target> Container<T>
+where
+    [(); T::LENGTH]: Sized,
+{
+    pub fn start(
+        _target: T,
+    ) -> Container<T> {
+        Container { _target }
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/issues/issue-89146.rs b/src/test/ui/const-generics/issues/issue-89146.rs
new file mode 100644
index 00000000000..e3540f46f1e
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-89146.rs
@@ -0,0 +1,26 @@
+// build-pass
+
+#![allow(incomplete_features)]
+#![feature(generic_const_exprs)]
+
+pub trait Foo {
+    const SIZE: usize;
+
+    fn to_bytes(&self) -> [u8; Self::SIZE];
+}
+
+pub fn bar<G: Foo>(a: &G) -> u8
+where
+    [(); G::SIZE]: Sized,
+{
+    deeper_bar(a)
+}
+
+fn deeper_bar<G: Foo>(a: &G) -> u8
+where
+    [(); G::SIZE]: Sized,
+{
+    a.to_bytes()[0]
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/issues/issue-89320.rs b/src/test/ui/const-generics/issues/issue-89320.rs
new file mode 100644
index 00000000000..afa5c8fab74
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-89320.rs
@@ -0,0 +1,19 @@
+// build-pass
+
+#![feature(generic_const_exprs)]
+#![allow(incomplete_features)]
+
+pub trait Enumerable {
+    const N: usize;
+}
+
+#[derive(Clone)]
+pub struct SymmetricGroup<S>
+where
+    S: Enumerable,
+    [(); S::N]: Sized,
+{
+    _phantom: std::marker::PhantomData<S>,
+}
+
+fn main() {}