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

Add support for relating slices in super_relate_consts #64858

Merged
merged 5 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub use self::error::{
InvalidProgramInfo, ResourceExhaustionInfo, UndefinedBehaviorInfo,
};

pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue};
pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue, get_slice_bytes};

pub use self::allocation::{Allocation, AllocationExtra, Relocations, UndefMask};

Expand Down
15 changes: 15 additions & 0 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,18 @@ impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {
Scalar(v),
Undef
});

/// Gets the bytes of a constant slice value.
pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
if let ConstValue::Slice { data, start, end } = val {
let len = end - start;
data.get_bytes(
cx,
// invent a pointer, only the offset is relevant anyway
Pointer::new(AllocId(0), Size::from_bytes(start as u64)),
Size::from_bytes(len as u64),
).unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
} else {
bug!("expected const slice, but found another const value");
}
}
17 changes: 15 additions & 2 deletions src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::hir::def_id::DefId;
use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
use crate::ty::error::{ExpectedFound, TypeError};
use crate::mir::interpret::{ConstValue, Scalar};
use crate::mir::interpret::{ConstValue, get_slice_bytes, Scalar};
use std::rc::Rc;
use std::iter;
use rustc_target::spec::abi;
Expand Down Expand Up @@ -584,7 +584,20 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
// FIXME(const_generics): we should either handle `Scalar::Ptr` or add a comment
// saying that we're not handling it intentionally.

// FIXME(const_generics): handle `ConstValue::ByRef` and `ConstValue::Slice`.
(a_val @ ConstValue::Slice { .. }, b_val @ 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 {
Ok(tcx.mk_const(ty::Const {
val: a_val,
ty: a.ty,
}))
} else {
Err(TypeError::ConstMismatch(expected_found(relation, &a, &b)))
}
}

// FIXME(const_generics): handle `ConstValue::ByRef`.

// FIXME(const_generics): this is wrong, as it is a projection
(ConstValue::Unevaluated(a_def_id, a_substs),
Expand Down
29 changes: 6 additions & 23 deletions src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use crate::hair::constant::*;
use rustc::lint;
use rustc::mir::{Field, BorrowKind, Mutability};
use rustc::mir::{UserTypeProjection};
use rustc::mir::interpret::{GlobalId, ConstValue, sign_extend, AllocId, Pointer};
use rustc::mir::interpret::{GlobalId, ConstValue, get_slice_bytes, sign_extend};
use rustc::traits::{ObligationCause, PredicateObligation};
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty, UserType, DefIdTree};
use rustc::ty::{CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations};
use rustc::ty::subst::{SubstsRef, GenericArg};
use rustc::ty::layout::{VariantIdx, Size};
use rustc::ty::layout::VariantIdx;
use rustc::hir::{self, RangeEnd};
use rustc::hir::def::{CtorOf, Res, DefKind, CtorKind};
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
Expand Down Expand Up @@ -1526,27 +1526,10 @@ pub fn compare_const_vals<'tcx>(

if let ty::Str = ty.kind {
match (a.val, b.val) {
(
ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },
ConstValue::Slice { data: alloc_b, start: offset_b, end: end_b },
) => {
let len_a = end_a - offset_a;
let len_b = end_b - offset_b;
let a = alloc_a.get_bytes(
&tcx,
// invent a pointer, only the offset is relevant anyway
Pointer::new(AllocId(0), Size::from_bytes(offset_a as u64)),
Size::from_bytes(len_a as u64),
);
let b = alloc_b.get_bytes(
&tcx,
// invent a pointer, only the offset is relevant anyway
Pointer::new(AllocId(0), Size::from_bytes(offset_b as u64)),
Size::from_bytes(len_b as u64),
);
if let (Ok(a), Ok(b)) = (a, b) {
return from_bool(a == b);
}
(ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
let a_bytes = get_slice_bytes(&tcx, a.val);
let b_bytes = get_slice_bytes(&tcx, b.val);
return from_bool(a_bytes == b_bytes);
}
_ => (),
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/const-generics/slice-const-param-mismatch.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 ConstString<const T: &'static str>;
struct ConstBytes<const T: &'static [u8]>;

pub fn main() {
let _: ConstString<"Hello"> = ConstString::<"World">; //~ ERROR mismatched types
let _: ConstBytes<b"AAA"> = ConstBytes::<{&[0x41, 0x41, 0x41]}>;
BenLewis-Seequent marked this conversation as resolved.
Show resolved Hide resolved
let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">; //~ ERROR mismatched types
}
29 changes: 29 additions & 0 deletions src/test/ui/const-generics/slice-const-param-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/slice-const-param-mismatch.rs:1:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error[E0308]: mismatched types
--> $DIR/slice-const-param-mismatch.rs:8:35
|
LL | let _: ConstString<"Hello"> = ConstString::<"World">;
| ^^^^^^^^^^^^^^^^^^^^^^ expected `"Hello"`, found `"World"`
|
= note: expected type `ConstString<>`
Copy link
Contributor

Choose a reason for hiding this comment

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

btw @varkor, this is a rather unfortunate diagnostic... do we have an issue for that?

Copy link
Author

Choose a reason for hiding this comment

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

There is a case for it: #61395.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah excellent, thanks!

found type `ConstString<>`

error[E0308]: mismatched types
--> $DIR/slice-const-param-mismatch.rs:10:33
|
LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
| ^^^^^^^^^^^^^^^^^^^^ expected `b"AAA"`, found `b"BBB"`
|
= note: expected type `ConstBytes<>`
found type `ConstBytes<>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/slice-const-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// run-pass

#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

pub fn function_with_str<const STRING: &'static str>() -> &'static str {
STRING
}

pub fn function_with_bytes<const BYTES: &'static [u8]>() -> &'static [u8] {
BYTES
}

pub fn main() {
assert_eq!(function_with_str::<"Rust">(), "Rust");
assert_eq!(function_with_bytes::<b"AAAA">(), &[0x41, 0x41, 0x41, 0x41]);
assert_eq!(function_with_bytes::<{&[0x41, 0x41, 0x41, 0x41]}>(), b"AAAA");
}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/slice-const-param.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/slice-const-param.rs:3:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default