1
Fork 0

Rollup merge of #109336 - compiler-errors:constrain-to-ct-err, r=BoxyUwU

Constrain const vars to error if const types are mismatched

When equating two consts of different types, if either are const variables, constrain them to the correct const error kind.

This helps us avoid "successfully" matching a const against an impl signature but leaving unconstrained const vars, which will lead to incremental ICEs when we call const-eval queries during const projection.

Fixes #109296

The second commit in the stack fixes a regression in the first commit where we end up mentioning `[const error]` in an impl overlap error message. I think the error message changes for the better, but I could implement alternative strategies to avoid this without delaying the overlap error message...

r? `@BoxyUwU`
This commit is contained in:
Matthias Krüger 2023-03-21 19:00:12 +01:00 committed by GitHub
commit 081c607b0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 13 deletions

View file

@ -189,10 +189,19 @@ impl<'tcx> InferCtxt<'tcx> {
// the expected const's type. Specifically, we don't want const infer vars // the expected const's type. Specifically, we don't want const infer vars
// to do any type shapeshifting before and after resolution. // to do any type shapeshifting before and after resolution.
if let Err(guar) = compatible_types { if let Err(guar) = compatible_types {
return Ok(self.tcx.const_error_with_guaranteed( // HACK: equating both sides with `[const error]` eagerly prevents us
if relation.a_is_expected() { a.ty() } else { b.ty() }, // from leaving unconstrained inference vars during things like impl
guar, // matching in the solver.
)); let a_error = self.tcx.const_error_with_guaranteed(a.ty(), guar);
if let ty::ConstKind::Infer(InferConst::Var(vid)) = a.kind() {
return self.unify_const_variable(vid, a_error);
}
let b_error = self.tcx.const_error_with_guaranteed(b.ty(), guar);
if let ty::ConstKind::Infer(InferConst::Var(vid)) = b.kind() {
return self.unify_const_variable(vid, b_error);
}
return Ok(if relation.a_is_expected() { a_error } else { b_error });
} }
match (a.kind(), b.kind()) { match (a.kind(), b.kind()) {

View file

@ -22,7 +22,7 @@ use crate::traits::{
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{error_code, DelayDm, Diagnostic}; use rustc_errors::{error_code, DelayDm, Diagnostic};
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt}; use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{InternalSubsts, SubstsRef}; use rustc_middle::ty::{InternalSubsts, SubstsRef};
use rustc_session::lint::builtin::COHERENCE_LEAK_CHECK; use rustc_session::lint::builtin::COHERENCE_LEAK_CHECK;
use rustc_session::lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS; use rustc_session::lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS;
@ -350,6 +350,10 @@ fn report_conflicting_impls<'tcx>(
impl_span: Span, impl_span: Span,
err: &mut Diagnostic, err: &mut Diagnostic,
) { ) {
if (overlap.trait_ref, overlap.self_ty).references_error() {
err.downgrade_to_delayed_bug();
}
match tcx.span_of_impl(overlap.with_impl) { match tcx.span_of_impl(overlap.with_impl) {
Ok(span) => { Ok(span) => {
err.span_label(span, "first implementation here"); err.span_label(span, "first implementation here");

View file

@ -0,0 +1,13 @@
// incremental
#![crate_type = "lib"]
trait Q {
const ASSOC: usize;
}
impl<const N: u64> Q for [u8; N] {
//~^ ERROR mismatched types
const ASSOC: usize = 1;
}
pub fn test() -> [u8; <[u8; 13] as Q>::ASSOC] { todo!() }

View file

@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/bad-subst-const-kind.rs:8:31
|
LL | impl<const N: u64> Q for [u8; N] {
| ^ expected `usize`, found `u64`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -2,11 +2,11 @@
// An impl that has an erroneous const substitution should not specialize one // An impl that has an erroneous const substitution should not specialize one
// that is well-formed. // that is well-formed.
#[derive(Clone)]
struct S<const L: usize>; struct S<const L: usize>;
impl<const N: i32> Copy for S<N> {} impl<const N: i32> Copy for S<N> {}
//~^ ERROR the constant `N` is not of type `usize`
impl<const M: usize> Copy for S<M> {} impl<const M: usize> Copy for S<M> {}
//~^ ERROR conflicting implementations of trait `Copy` for type `S<_>`
fn main() {} fn main() {}

View file

@ -1,11 +1,14 @@
error[E0119]: conflicting implementations of trait `Copy` for type `S<_>` error: the constant `N` is not of type `usize`
--> $DIR/bad-const-wf-doesnt-specialize.rs:9:1 --> $DIR/bad-const-wf-doesnt-specialize.rs:8:29
| |
LL | impl<const N: i32> Copy for S<N> {} LL | impl<const N: i32> Copy for S<N> {}
| -------------------------------- first implementation here | ^^^^
LL | impl<const M: usize> Copy for S<M> {} |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `S<_>` note: required by a bound in `S`
--> $DIR/bad-const-wf-doesnt-specialize.rs:6:10
|
LL | struct S<const L: usize>;
| ^^^^^^^^^^^^^^ required by this bound in `S`
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0119`.