Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
11 changes: 3 additions & 8 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ even other Rust compilers, such as rust-analyzer!

*/

use std::cmp::min;
use std::fmt;
#[cfg(feature = "nightly")]
use std::iter::Step;
Expand Down Expand Up @@ -1060,14 +1061,8 @@ impl Align {
/// Either `1 << (pointer_bits - 1)` or [`Align::MAX`], whichever is smaller.
#[inline]
pub fn max_for_target(tdl: &TargetDataLayout) -> Align {
let pointer_bits = tdl.pointer_size().bits();
if let Ok(pointer_bits) = u8::try_from(pointer_bits)
&& pointer_bits <= Align::MAX.pow2
{
Align { pow2: pointer_bits - 1 }
} else {
Align::MAX
}
let pointer_bits = u8::try_from(tdl.pointer_size().bits()).unwrap();
min(Align { pow2: pointer_bits - 1 }, Align::MAX)
}

#[inline]
Expand Down
91 changes: 60 additions & 31 deletions compiler/rustc_const_eval/src/const_eval/valtrees.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_abi::{BackendRepr, FieldIdx, VariantIdx};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId, ValTreeCreationError};
use rustc_middle::traits::ObligationCause;
Expand All @@ -17,13 +18,15 @@ use crate::interpret::{
intern_const_alloc_recursive,
};

#[instrument(skip(ecx), level = "debug")]
#[instrument(skip(ecx, visited, settled), level = "debug")]
fn branches<'tcx>(
ecx: &CompileTimeInterpCx<'tcx>,
place: &MPlaceTy<'tcx>,
field_count: usize,
variant: Option<VariantIdx>,
num_nodes: &mut usize,
visited: &mut FxHashSet<MPlaceTy<'tcx>>,
settled: &mut FxHashMap<MPlaceTy<'tcx>, EvalToValTreeResult<'tcx>>,
) -> EvalToValTreeResult<'tcx> {
let place = match variant {
Some(variant) => ecx.project_downcast(place, variant).unwrap(),
Expand All @@ -45,7 +48,7 @@ fn branches<'tcx>(

for i in 0..field_count {
let field = ecx.project_field(&place, FieldIdx::from_usize(i)).unwrap();
let valtree = const_to_valtree_inner(ecx, &field, num_nodes)?;
let valtree = const_to_valtree_inner(ecx, &field, num_nodes, visited, settled)?;
branches.push(ty::Const::new_value(*ecx.tcx, valtree, field.layout.ty));
}

Expand All @@ -57,39 +60,53 @@ fn branches<'tcx>(
Ok(ty::ValTree::from_branches(*ecx.tcx, branches))
}

#[instrument(skip(ecx), level = "debug")]
#[instrument(skip(ecx, visited, settled), level = "debug")]
fn slice_branches<'tcx>(
ecx: &CompileTimeInterpCx<'tcx>,
place: &MPlaceTy<'tcx>,
num_nodes: &mut usize,
visited: &mut FxHashSet<MPlaceTy<'tcx>>,
settled: &mut FxHashMap<MPlaceTy<'tcx>, EvalToValTreeResult<'tcx>>,
) -> EvalToValTreeResult<'tcx> {
let n = place.len(ecx).unwrap_or_else(|_| panic!("expected to use len of place {place:?}"));

let mut elems = Vec::with_capacity(n as usize);
for i in 0..n {
let place_elem = ecx.project_index(place, i).unwrap();
let valtree = const_to_valtree_inner(ecx, &place_elem, num_nodes)?;
let valtree = const_to_valtree_inner(ecx, &place_elem, num_nodes, visited, settled)?;
elems.push(ty::Const::new_value(*ecx.tcx, valtree, place_elem.layout.ty));
}

Ok(ty::ValTree::from_branches(*ecx.tcx, elems))
}

#[instrument(skip(ecx), level = "debug")]
#[instrument(skip(ecx, visited, settled), level = "debug")]
fn const_to_valtree_inner<'tcx>(
ecx: &CompileTimeInterpCx<'tcx>,
place: &MPlaceTy<'tcx>,
num_nodes: &mut usize,
visited: &mut FxHashSet<MPlaceTy<'tcx>>,
settled: &mut FxHashMap<MPlaceTy<'tcx>, EvalToValTreeResult<'tcx>>,
) -> EvalToValTreeResult<'tcx> {
let tcx = *ecx.tcx;
let ty = place.layout.ty;
debug!("ty kind: {:?}", ty.kind());

if let Some(&result) = settled.get(place) {
return result;
}

if visited.contains(place) {
return Err(ValTreeCreationError::CyclicConst);
}

if *num_nodes >= VALTREE_MAX_NODES {
return Err(ValTreeCreationError::NodesOverflow);
}

match ty.kind() {
visited.insert(place.clone());

let result = ensure_sufficient_stack(|| match ty.kind() {
ty::FnDef(..) => {
*num_nodes += 1;
Ok(ty::ValTree::zst(tcx))
Expand All @@ -108,7 +125,7 @@ fn const_to_valtree_inner<'tcx>(
// Since the returned valtree does not contain the type or layout, we can just
// switch to the base type.
place.layout = ecx.layout_of(*base).unwrap();
ensure_sufficient_stack(|| const_to_valtree_inner(ecx, &place, num_nodes))
const_to_valtree_inner(ecx, &place, num_nodes, visited, settled)
}

ty::RawPtr(_, _) => {
Expand All @@ -120,16 +137,16 @@ fn const_to_valtree_inner<'tcx>(
// We could allow wide raw pointers where both sides are integers in the future,
// but for now we reject them.
if matches!(val.layout.backend_repr, BackendRepr::ScalarPair(..)) {
return Err(ValTreeCreationError::NonSupportedType(ty));
Err(ValTreeCreationError::NonSupportedType(ty))
} else {
let val = val.to_scalar();
// We are in the CTFE machine, so ptr-to-int casts will fail.
// This can only be `Ok` if `val` already is an integer.
match val.try_to_scalar_int() {
Ok(val) => Ok(ty::ValTree::from_scalar_int(tcx, val)),
Err(_) => Err(ValTreeCreationError::NonSupportedType(ty)),
}
}
let val = val.to_scalar();
// We are in the CTFE machine, so ptr-to-int casts will fail.
// This can only be `Ok` if `val` already is an integer.
let Ok(val) = val.try_to_scalar_int() else {
return Err(ValTreeCreationError::NonSupportedType(ty));
};
// It's just a ScalarInt!
Ok(ty::ValTree::from_scalar_int(tcx, val))
}

// Technically we could allow function pointers (represented as `ty::Instance`), but this is not guaranteed to
Expand All @@ -138,33 +155,39 @@ fn const_to_valtree_inner<'tcx>(

ty::Ref(_, _, _) => {
let derefd_place = ecx.deref_pointer(place).report_err()?;
const_to_valtree_inner(ecx, &derefd_place, num_nodes)
const_to_valtree_inner(ecx, &derefd_place, num_nodes, visited, settled)
}

ty::Str | ty::Slice(_) | ty::Array(_, _) => slice_branches(ecx, place, num_nodes),
ty::Str | ty::Slice(_) | ty::Array(_, _) => {
slice_branches(ecx, place, num_nodes, visited, settled)
}
// 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 StructuralPartialEq` trait objects in the future,
// but it is unclear if this is useful.
ty::Dynamic(..) => Err(ValTreeCreationError::NonSupportedType(ty)),

ty::Tuple(elem_tys) => branches(ecx, place, elem_tys.len(), None, num_nodes),
ty::Tuple(elem_tys) => {
branches(ecx, place, elem_tys.len(), None, num_nodes, visited, settled)
}

ty::Adt(def, _) => {
if def.is_union() {
return Err(ValTreeCreationError::NonSupportedType(ty));
Err(ValTreeCreationError::NonSupportedType(ty))
} else if def.variants().is_empty() {
bug!("uninhabited types should have errored and never gotten converted to valtree")
} else {
let variant = ecx.read_discriminant(place).report_err()?;
branches(
ecx,
place,
def.variant(variant).fields.len(),
def.is_enum().then_some(variant),
num_nodes,
visited,
settled,
)
}

let variant = ecx.read_discriminant(place).report_err()?;
branches(
ecx,
place,
def.variant(variant).fields.len(),
def.is_enum().then_some(variant),
num_nodes,
)
}

// FIXME(oli-obk): we could look behind opaque types
Expand All @@ -186,7 +209,11 @@ fn const_to_valtree_inner<'tcx>(
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
| ty::UnsafeBinder(_) => Err(ValTreeCreationError::NonSupportedType(ty)),
}
});

visited.remove(place);
settled.insert(place.clone(), result);
result
}

/// Valtrees don't store the `MemPlaceMeta` that all dynamically sized values have in the interpreter.
Expand Down Expand Up @@ -257,7 +284,9 @@ pub(crate) fn eval_to_valtree<'tcx>(
debug!(?place);

let mut num_nodes = 0;
const_to_valtree_inner(&ecx, &place, &mut num_nodes)
let mut visited = FxHashSet::default();
let mut settled = FxHashMap::default();
const_to_valtree_inner(&ecx, &place, &mut num_nodes, &mut visited, &mut settled)
}

/// Converts a `ValTree` to a `ConstValue`, which is needed after mir
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ pub(crate) struct InvalidConstInValtree {
pub global_const_id: String,
}

#[derive(Diagnostic)]
#[diag("constant {$global_const_id} cannot be used as pattern")]
#[note("constants whose type references itself cannot be used as patterns")]
pub(crate) struct CyclicConstInValtree {
#[primary_span]
pub span: Span,
pub global_const_id: String,
}

#[derive(Diagnostic)]
#[diag("internal compiler error: reentrant incremental verify failure, suppressing message")]
pub(crate) struct Reentrant;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub enum ValTreeCreationError<'tcx> {
InvalidConst,
/// Values of this type, or this particular value, are not supported as valtrees.
NonSupportedType(Ty<'tcx>),
/// Trying to valtree this constant would cause the valtree to have cycles.
CyclicConst,
/// The error has already been handled by const evaluation.
ErrorHandled(ErrorHandled),
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ impl<'tcx> TyCtxt<'tcx> {
});
Err(ReportedErrorInfo::allowed_in_infallible(handled).into())
}
ValTreeCreationError::CyclicConst => {
let handled = self.dcx().emit_err(error::CyclicConstInValtree {
span,
global_const_id: cid.display(self),
});
Err(ReportedErrorInfo::allowed_in_infallible(handled).into())
}
ValTreeCreationError::ErrorHandled(handled) => Err(handled),
}
}
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,23 @@ impl<'tcx> TypingEnv<'tcx> {
Self::new(tcx.param_env(def_id), TypingMode::non_body_analysis())
}

/// Ideally we just use `TypingMode::PostTypeckUntilBorrowck`.
/// But that's not compatible with the old solver yet.
///
/// FIXME: this should not be needed in the long term.
pub fn post_typeck_until_borrowck_for_mir_build(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> TypingEnv<'tcx> {
if tcx.use_typing_mode_post_typeck_until_borrowck() {
TypingEnv::new(tcx.param_env(def_id.to_def_id()), ty::TypingMode::borrowck(tcx, def_id))
} else {
// FIXME(#132279): We're in a body, we should use a typing
// mode which reveals the opaque types defined by that body.
TypingEnv::non_body_analysis(tcx, def_id)
}
}

pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey<DefId>) -> TypingEnv<'tcx> {
TypingEnv::new(tcx.param_env_normalized_for_post_analysis(def_id), TypingMode::PostAnalysis)
}
Expand Down
24 changes: 18 additions & 6 deletions compiler/rustc_mir_build/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,15 @@ fn construct_fn<'tcx>(
);
}

// FIXME(#132279): This should be able to reveal opaque
// types defined during HIR typeck.
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
let typing_mode = if tcx.use_typing_mode_post_typeck_until_borrowck() {
TypingMode::borrowck(tcx, fn_def)
} else {
// FIXME(#132279): This should be able to reveal opaque
// types defined during HIR typeck.
TypingMode::non_body_analysis()
};

let infcx = tcx.infer_ctxt().build(typing_mode);
let mut builder = Builder::new(
thir,
infcx,
Expand Down Expand Up @@ -587,9 +593,15 @@ fn construct_const<'a, 'tcx>(
_ => span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def),
};

// FIXME(#132279): We likely want to be able to use the hidden types of
// opaques used by this function here.
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
let typing_mode = if tcx.use_typing_mode_post_typeck_until_borrowck() {
TypingMode::borrowck(tcx, def)
} else {
// FIXME(#132279): This should be able to reveal opaque
// types defined during HIR typeck.
TypingMode::non_body_analysis()
};

let infcx = tcx.infer_ctxt().build(typing_mode);
let mut builder =
Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);

Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_mir_build/src/builder/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
tcx: self.tcx,
typeck_results,
module: self.tcx.parent_module(self.hir_id).to_def_id(),
// FIXME(#132279): We're in a body, should handle opaques.
typing_env: rustc_middle::ty::TypingEnv::non_body_analysis(self.tcx, self.def_id),
typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(
self.tcx,
self.def_id,
),
dropless_arena: &dropless_arena,
match_lint_level: self.hir_id,
whole_match_span: Some(rustc_span::Span::default()),
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_build/src/check_tail_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), E
tcx,
thir,
found_errors: Ok(()),
// FIXME(#132279): we're clearly in a body here.
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def),
is_closure,
caller_def_id: def,
};
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,7 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
body_target_features,
assignment_info: None,
in_union_destructure: false,
// FIXME(#132279): we're clearly in a body here.
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def),
inside_adt: false,
warnings: &mut warnings,
suggest_unsafe_block: true,
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_build/src/thir/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
Self {
tcx,
thir: Thir::new(body_type),
// FIXME(#132279): We're in a body, we should use a typing
// mode which reveals the opaque types defined by that body.
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def),
typeck_results,
body_owner: def.to_def_id(),
apply_adjustments: !find_attr!(tcx, hir_id, CustomMir(..)),
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), Err
tcx,
thir: &*thir,
typeck_results,
// FIXME(#132279): We're in a body, should handle opaques.
typing_env: ty::TypingEnv::non_body_analysis(tcx, def_id),
typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def_id),
hir_source: tcx.local_def_id_to_hir_id(def_id),
let_source: LetSource::None,
pattern_arena: &pattern_arena,
Expand Down
Loading
Loading