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
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 16 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,22 @@ dependencies = [
"atty",
"bitflags",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim 0.10.0",
"termcolor",
"textwrap 0.14.2",
]

[[package]]
name = "clap_complete"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df6f3613c0a3cddfd78b41b10203eb322cb29b600cbdf808a7d3db95691b8e25"
dependencies = [
"clap 3.1.1",
]

[[package]]
name = "clippy"
version = "0.1.62"
Expand Down Expand Up @@ -2240,14 +2250,15 @@ dependencies = [

[[package]]
name = "mdbook"
version = "0.4.15"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "241f10687eb3b4e0634b3b4e423f97c5f1efbd69dc9522e24a8b94583eeec3c6"
checksum = "74612ae81a3e5ee509854049dfa4c7975ae033c06f5fc4735c7dfbe60ee2a39d"
dependencies = [
"ammonia",
"anyhow",
"chrono",
"clap 2.34.0",
"clap 3.1.1",
"clap_complete",
"elasticlunr-rs",
"env_logger 0.7.1",
"handlebars",
Expand Down Expand Up @@ -2911,7 +2922,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34f197a544b0c9ab3ae46c359a7ec9cbbb5c7bf97054266fecb7ead794a181d6"
dependencies = [
"bitflags",
"getopts",
"memchr",
"unicase",
]
Expand Down Expand Up @@ -3129,9 +3139,9 @@ dependencies = [

[[package]]
name = "regex"
version = "1.5.4"
version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [
"aho-corasick",
"memchr",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ impl<'a> State<'a> {
self.word_space("=");
match term {
Term::Ty(ty) => self.print_type(ty),
Term::Const(c) => self.print_expr_anon_const(c),
Term::Const(c) => self.print_expr_anon_const(c, &[]),
}
}
ast::AssocConstraintKind::Bound { bounds } => self.print_type_bounds(":", &*bounds),
Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,21 @@ impl<'a> State<'a> {
self.end();
}

pub(super) fn print_expr_anon_const(&mut self, expr: &ast::AnonConst) {
pub(super) fn print_expr_anon_const(
&mut self,
expr: &ast::AnonConst,
attrs: &[ast::Attribute],
) {
self.ibox(INDENT_UNIT);
self.word("const");
self.print_expr(&expr.value);
self.nbsp();
if let ast::ExprKind::Block(block, None) = &expr.value.kind {
self.cbox(0);
self.ibox(0);
self.print_block_with_attrs(block, attrs);
} else {
self.print_expr(&expr.value);
}
self.end();
}

Expand Down Expand Up @@ -275,7 +286,7 @@ impl<'a> State<'a> {
self.print_expr_vec(exprs);
}
ast::ExprKind::ConstBlock(ref anon_const) => {
self.print_expr_anon_const(anon_const);
self.print_expr_anon_const(anon_const, attrs);
}
ast::ExprKind::Repeat(ref element, ref count) => {
self.print_expr_repeat(element, count);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub(super) fn op_to_const<'tcx>(
}
}

#[instrument(skip(tcx), level = "debug")]
fn turn_into_const_value<'tcx>(
tcx: TyCtxt<'tcx>,
constant: ConstAlloc<'tcx>,
Expand All @@ -206,6 +207,7 @@ fn turn_into_const_value<'tcx>(
!is_static || cid.promoted.is_some(),
"the `eval_to_const_value_raw` query should not be used for statics, use `eval_to_allocation` instead"
);

// Turn this into a proper constant.
op_to_const(&ecx, &mplace.into())
}
Expand Down
80 changes: 55 additions & 25 deletions compiler/rustc_const_eval/src/const_eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
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},
ty::ScalarInt,
};
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
use rustc_target::abi::VariantIdx;

use crate::interpret::{
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MPlaceTy,
Expand Down Expand Up @@ -55,28 +57,48 @@ pub(crate) fn const_to_valtree<'tcx>(
const_to_valtree_inner(&ecx, &place)
}

fn const_to_valtree_inner<'tcx>(
#[instrument(skip(ecx), level = "debug")]
fn branches<'tcx>(
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
place: &MPlaceTy<'tcx>,
n: usize,
variant: Option<VariantIdx>,
) -> Option<ty::ValTree<'tcx>> {
let branches = |n, variant| {
let place = match variant {
Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
None => *place,
};
let variant =
variant.map(|variant| Some(ty::ValTree::Leaf(ScalarInt::from(variant.as_u32()))));
let fields = (0..n).map(|i| {
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
// 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<_>>>()?),
))
let place = match variant {
Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
None => *place,
};
let variant = variant.map(|variant| Some(ty::ValTree::Leaf(ScalarInt::from(variant.as_u32()))));
debug!(?place, ?variant);

let fields = (0..n).map(|i| {
let field = ecx.mplace_field(&place, i).unwrap();
const_to_valtree_inner(ecx, &field)
});
// 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<_>>>()?)))
}

fn slice_branches<'tcx>(
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
place: &MPlaceTy<'tcx>,
) -> Option<ty::ValTree<'tcx>> {
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)
});

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

#[instrument(skip(ecx), level = "debug")]
fn const_to_valtree_inner<'tcx>(
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
place: &MPlaceTy<'tcx>,
) -> Option<ty::ValTree<'tcx>> {
match place.layout.ty.kind() {
ty::FnDef(..) => Some(ty::ValTree::zst()),
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
Expand All @@ -90,19 +112,27 @@ fn const_to_valtree_inner<'tcx>(
// Technically we could allow function pointers (represented as `ty::Instance`), but this is not guaranteed to
// agree with runtime equality tests.
ty::FnPtr(_) | ty::RawPtr(_) => None,
ty::Ref(..) => unimplemented!("need to use deref_const"),

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)
}

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::Slice(_) | ty::Str => {
unimplemented!("need to find the backing data of the slice/str and recurse on that")
}
ty::Tuple(substs) => branches(substs.len(), None),
ty::Array(_, len) => branches(usize::try_from(len.eval_usize(ecx.tcx.tcx, ecx.param_env)).unwrap(), None),
ty::Tuple(substs) => branches(ecx, place, substs.len(), None),

ty::Adt(def, _) => {
if def.variants().is_empty() {
Expand All @@ -111,7 +141,7 @@ fn const_to_valtree_inner<'tcx>(

let variant = ecx.read_discriminant(&place.into()).unwrap().1;

branches(def.variant(variant).fields.len(), def.is_enum().then_some(variant))
branches(ecx, place, def.variant(variant).fields.len(), def.is_enum().then_some(variant))
}

ty::Never
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'tcx, Tag: Provenance> MPlaceTy<'tcx, Tag> {
}

#[inline]
pub(super) fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
pub(crate) fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
if self.layout.is_unsized() {
// We need to consult `meta` metadata
match self.layout.ty.kind() {
Expand Down
Loading