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

Rollup of 7 pull requests #96123

Merged
merged 19 commits into from
Apr 16, 2022
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f427698
Parse inner attributes on inline const block
dtolnay Mar 16, 2022
5e76103
Reject `#[thread_local]` attribute on non-static items
tmiasko Mar 16, 2022
51aa3f8
Add type for slices in ValTrees
b-naber Mar 29, 2022
fcc4d8c
include refs in valtree creation
b-naber Mar 29, 2022
82217a6
create leafs for slices
b-naber Apr 8, 2022
8a5273b
use deref on ImmTy
b-naber Apr 8, 2022
edeb826
Inline shallow_resolve_ty into ShallowResolver
compiler-errors Apr 10, 2022
4b126b8
use len on mplace instead of reading immediate, remove dead code
b-naber Apr 12, 2022
d8205cd
handle arrays and slices uniformly in valtree creation
b-naber Apr 14, 2022
219d81f
separate flock implementations into separate modules
euclio Apr 14, 2022
5181422
Update mdbook
ehuss Apr 15, 2022
b1e6211
Rename `def_id` into `item_id` when the type is `ItemId` for readability
GuillaumeGomez Apr 16, 2022
22d5546
Rollup merge of #94985 - dtolnay:constattr, r=pnkfelix
Dylan-DPC Apr 16, 2022
3dced80
Rollup merge of #95006 - tmiasko:thread-local-static, r=wesleywiser
Dylan-DPC Apr 16, 2022
1dc672a
Rollup merge of #95426 - b-naber:valtrees-slice, r=RalfJung,oli-obk
Dylan-DPC Apr 16, 2022
a84a811
Rollup merge of #95908 - compiler-errors:shallow_resolve_ty-inline, r…
Dylan-DPC Apr 16, 2022
9905774
Rollup merge of #96058 - euclio:flock-impls, r=nagisa
Dylan-DPC Apr 16, 2022
5d98ce6
Rollup merge of #96088 - ehuss:update-mdbook, r=Mark-Simulacrum
Dylan-DPC Apr 16, 2022
10e0db5
Rollup merge of #96118 - GuillaumeGomez:cleanup-def-id-item-id, r=not…
Dylan-DPC Apr 16, 2022
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
Prev Previous commit
Next Next commit
handle arrays and slices uniformly in valtree creation
b-naber committed Apr 14, 2022

Verified

This commit was signed with the committer’s verified signature.
targos Michaël Zasso
commit d8205cd3fe3cda6ebbcff40c8d173f8e05a375be
44 changes: 13 additions & 31 deletions compiler/rustc_const_eval/src/const_eval/mod.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
use std::convert::TryFrom;

use rustc_hir::Mutability;
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::{
mir::{self, interpret::ConstAlloc},
@@ -74,7 +75,7 @@ fn branches<'tcx>(
let field = ecx.mplace_field(&place, i).unwrap();
const_to_valtree_inner(ecx, &field)
});
// For enums, we preped their variant index before the variant's fields so we can figure out
// For enums, we prepend their variant index before the variant's fields so we can figure out
// the variant again when just seeing a valtree.
let branches = variant.into_iter().chain(fields);
Some(ty::ValTree::Branch(ecx.tcx.arena.alloc_from_iter(branches.collect::<Option<Vec<_>>>()?)))
@@ -83,17 +84,13 @@ fn branches<'tcx>(
fn slice_branches<'tcx>(
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
place: &MPlaceTy<'tcx>,
n: u64,
) -> Option<ty::ValTree<'tcx>> {
let elems = (0..n).map(|i| {
let n = place.len(&ecx.tcx()).expect(&format!("expected to use len of place {:?}", place));
let branches = (0..n).map(|i| {
let place_elem = ecx.mplace_index(place, i).unwrap();
const_to_valtree_inner(ecx, &place_elem)
});

// Need `len` for the ValTree -> ConstValue conversion
let len = Some(Some(ty::ValTree::Leaf(ScalarInt::from(n))));
let branches = len.into_iter().chain(elems);

Some(ty::ValTree::Branch(ecx.tcx.arena.alloc_from_iter(branches.collect::<Option<Vec<_>>>()?)))
}

@@ -116,41 +113,26 @@ fn const_to_valtree_inner<'tcx>(
// agree with runtime equality tests.
ty::FnPtr(_) | ty::RawPtr(_) => None,

ty::Ref(_, inner_ty, _) => {
match inner_ty.kind() {
ty::Slice(_) | ty::Str => {
let derefd = ecx.deref_operand(&place.into()).unwrap();
debug!(?derefd);
let len = derefd.len(&ecx.tcx.tcx).unwrap();
let valtree = slice_branches(ecx, &derefd, len);
debug!(?valtree);

valtree
}
_ => {
let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
debug!(?derefd_place);
ty::Ref(_, _, _) => {
let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
debug!(?derefd_place);

const_to_valtree_inner(ecx, &derefd_place)
}
}
const_to_valtree_inner(ecx, &derefd_place)
}

ty::Str => {
bug!("ty::Str should have been handled in ty::Ref branch that uses raw bytes");
}
ty::Slice(_) => {
bug!("should have been handled in the Ref arm");
}
ty::Str | ty::Slice(_) | ty::Array(_, _) => {
let valtree = slice_branches(ecx, place);
debug!(?valtree);

valtree
}
// Trait objects are not allowed in type level constants, as we have no concept for
// resolving their backing type, even if we can do that at const eval time. We may
// hypothetically be able to allow `dyn StructuralEq` trait objects in the future,
// but it is unclear if this is useful.
ty::Dynamic(..) => None,

ty::Tuple(substs) => branches(ecx, place, substs.len(), None),
ty::Array(_, len) => branches(ecx, place, usize::try_from(len.eval_usize(ecx.tcx.tcx, ecx.param_env)).unwrap(), None),

ty::Adt(def, _) => {
if def.variants().is_empty() {