Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 30 additions & 3 deletions src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use crate::hir::def_id::DefId;
use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
use crate::ty::{self, layout::Size, Ty, TyCtxt, TypeFoldable};
use crate::ty::error::{ExpectedFound, TypeError};
use crate::mir::interpret::{ConstValue, Scalar};
use crate::mir::interpret::{AllocId, ConstValue, Pointer, Scalar};
use std::rc::Rc;
use std::iter;
use rustc_target::spec::abi;
Expand Down Expand Up @@ -584,7 +584,34 @@ 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 { .. }) => {
fn get_slice_bytes<'tcx>(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>) -> &'tcx [u8] {
if let ConstValue::Slice { data, start, end } = val {
let len = end - start;
data.get_bytes(
&tcx,
// 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 {
unreachable!();
}
}

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
12 changes: 12 additions & 0 deletions src/test/ui/const-generics/str-const-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// run-pass
Comment thread
BenLewis-Seequent marked this conversation as resolved.
Outdated

#![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 main() {
assert_eq!(function_with_str::<"Rust">(), "Rust");
}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/str-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/str-const-param.rs:3:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default