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

handle ConstValue::ByRef in relate #71018

Merged
merged 2 commits into from
May 2, 2020
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
1 change: 0 additions & 1 deletion src/librustc_infer/infer/at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ impl<'a, 'tcx> At<'a, 'tcx> {
impl<'a, 'tcx> Trace<'a, 'tcx> {
/// Makes `a <: b` where `a` may or may not be expected (if
/// `a_is_expected` is true, then `a` is expected).
/// Makes `expected <: actual`.
pub fn sub<T>(self, a: &T, b: &T) -> InferResult<'tcx, ()>
where
T: Relate<'tcx>,
Expand Down
36 changes: 34 additions & 2 deletions src/librustc_middle/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc_hir as ast;
use rustc_hir::def_id::DefId;
use rustc_span::DUMMY_SP;
use rustc_target::spec::abi;
use std::iter;
use std::rc::Rc;
Expand Down Expand Up @@ -507,6 +508,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
a: &'tcx ty::Const<'tcx>,
b: &'tcx ty::Const<'tcx>,
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
debug!("{}.super_relate_consts(a = {:?}, b = {:?})", relation.tag(), a, b);
let tcx = relation.tcx();

let eagerly_eval = |x: &'tcx ty::Const<'tcx>| {
Expand Down Expand Up @@ -561,7 +563,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
}
}

(a_val @ ConstValue::Slice { .. }, b_val @ ConstValue::Slice { .. }) => {
(ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
let a_bytes = get_slice_bytes(&tcx, a_val);
let b_bytes = get_slice_bytes(&tcx, b_val);
if a_bytes == b_bytes {
Expand All @@ -571,7 +573,37 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
}
}

// FIXME(const_generics): handle `ConstValue::ByRef`.
(ConstValue::ByRef { .. }, ConstValue::ByRef { .. }) => {
match a.ty.kind {
ty::Array(..) | ty::Adt(..) | ty::Tuple(..) => {
let a_destructured = tcx.destructure_const(relation.param_env().and(a));
let b_destructured = tcx.destructure_const(relation.param_env().and(b));

// Both the variant and each field have to be equal.
if a_destructured.variant == b_destructured.variant {
for (a_field, b_field) in
a_destructured.fields.iter().zip(b_destructured.fields.iter())
{
relation.consts(a_field, b_field)?;
}

Ok(a_val)
} else {
Err(TypeError::ConstMismatch(expected_found(relation, &a, &b)))
}
}
// FIXME(const_generics): There are probably some `TyKind`s
// which should be handled here.
_ => {
tcx.sess.delay_span_bug(
DUMMY_SP,
&format!("unexpected consts: a: {:?}, b: {:?}", a, b),
);
Err(TypeError::ConstMismatch(expected_found(relation, &a, &b)))
}
}
}

_ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))),
};

Expand Down
3 changes: 3 additions & 0 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fn coerce_mutbls<'tcx>(
}
}

/// Do not require any adjustments, i.e. coerce `x -> x`.
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
fn identity(_: Ty<'_>) -> Vec<Adjustment<'_>> {
vec![]
}
Expand All @@ -115,6 +116,7 @@ fn simple(kind: Adjust<'tcx>) -> impl FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>
move |target| vec![Adjustment { kind, target }]
}

/// This always returns `Ok(...)`.
fn success<'tcx>(
adj: Vec<Adjustment<'tcx>>,
target: Ty<'tcx>,
Expand All @@ -133,6 +135,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}

pub fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
self.commit_if_ok(|_| {
if self.use_lub {
self.at(&self.cause, self.fcx.param_env).lub(b, a)
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/const-generics/different_byref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

struct Const<const V: [usize; 1]> {}

fn main() {
let mut x = Const::<{ [3] }> {};
x = Const::<{ [4] }> {};
//~^ ERROR mismatched types

}
20 changes: 20 additions & 0 deletions src/test/ui/const-generics/different_byref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/different_byref.rs:1:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error[E0308]: mismatched types
--> $DIR/different_byref.rs:8:9
|
LL | x = Const::<{ [4] }> {};
| ^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `4usize`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only mildly related to this PR

@rust-lang/wg-const-eval we could consider collecting all spans that lead to a value during const eval and thus allow each byte in the final constant to have its own spans for diagnostics. Then we can report a sort of backtrace for values. Though I fear that will cause quite some overhead, even if we only need to do it in the current crate and not serialize it to metadata (so ClearCrossCrate).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be more advanced than what we do for type aliases, heh.

|
= note: expected struct `Const<[3usize]>`
found struct `Const<[4usize]>`

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0308`.
11 changes: 11 additions & 0 deletions src/test/ui/const-generics/issues/issue-68615-adt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

struct Const<const V: [usize; 0]> {}
type MyConst = Const<{ [] }>;

fn main() {
let _x = Const::<{ [] }> {};
let _y = MyConst {};
}
11 changes: 11 additions & 0 deletions src/test/ui/const-generics/issues/issue-68615-array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

struct Foo<const V: [usize; 0] > {}

type MyFoo = Foo<{ [] }>;

fn main() {
let _ = Foo::<{ [] }> {};
}