Skip to content

Commit cd4cf56

Browse files
Remove mir::Const::from_anon_const
1 parent 7b4b1b0 commit cd4cf56

File tree

3 files changed

+29
-103
lines changed

3 files changed

+29
-103
lines changed

compiler/rustc_middle/src/mir/consts.rs

+3-99
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use std::fmt::{self, Debug, Display, Formatter};
22

33
use rustc_hir;
4-
use rustc_hir::def_id::{DefId, LocalDefId};
5-
use rustc_hir::{self as hir};
4+
use rustc_hir::def_id::DefId;
65
use rustc_span::Span;
76
use rustc_target::abi::{HasDataLayout, Size};
87

98
use crate::mir::interpret::{alloc_range, AllocId, ConstAllocation, ErrorHandled, Scalar};
109
use crate::mir::{pretty_print_const_value, Promoted};
10+
use crate::ty::GenericArgsRef;
1111
use crate::ty::ScalarInt;
12-
use crate::ty::{self, print::pretty_print_const, List, Ty, TyCtxt};
13-
use crate::ty::{GenericArgs, GenericArgsRef};
12+
use crate::ty::{self, print::pretty_print_const, Ty, TyCtxt};
1413

1514
///////////////////////////////////////////////////////////////////////////
1615
/// Evaluated Constants
@@ -371,101 +370,6 @@ impl<'tcx> Const<'tcx> {
371370
Self::Val(val, ty)
372371
}
373372

374-
/// Literals are converted to `Const::Val`, const generic parameters are eagerly
375-
/// converted to a constant, everything else becomes `Unevaluated`.
376-
#[instrument(skip(tcx), level = "debug", ret)]
377-
pub fn from_anon_const(
378-
tcx: TyCtxt<'tcx>,
379-
def: LocalDefId,
380-
param_env: ty::ParamEnv<'tcx>,
381-
) -> Self {
382-
let body_id = match tcx.hir().get_by_def_id(def) {
383-
hir::Node::AnonConst(ac) => ac.body,
384-
_ => {
385-
span_bug!(tcx.def_span(def), "from_anon_const can only process anonymous constants")
386-
}
387-
};
388-
389-
let expr = &tcx.hir().body(body_id).value;
390-
debug!(?expr);
391-
392-
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
393-
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
394-
let expr = match &expr.kind {
395-
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
396-
block.expr.as_ref().unwrap()
397-
}
398-
_ => expr,
399-
};
400-
debug!("expr.kind: {:?}", expr.kind);
401-
402-
let ty = tcx.type_of(def).instantiate_identity();
403-
debug!(?ty);
404-
405-
// FIXME(const_generics): We currently have to special case parameters because `min_const_generics`
406-
// does not provide the parents generics to anonymous constants. We still allow generic const
407-
// parameters by themselves however, e.g. `N`. These constants would cause an ICE if we were to
408-
// ever try to substitute the generic parameters in their bodies.
409-
//
410-
// While this doesn't happen as these constants are always used as `ty::ConstKind::Param`, it does
411-
// cause issues if we were to remove that special-case and try to evaluate the constant instead.
412-
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
413-
match expr.kind {
414-
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
415-
// Find the name and index of the const parameter by indexing the generics of
416-
// the parent item and construct a `ParamConst`.
417-
let item_def_id = tcx.parent(def_id);
418-
let generics = tcx.generics_of(item_def_id);
419-
let index = generics.param_def_id_to_index[&def_id];
420-
let name = tcx.item_name(def_id);
421-
let ty_const = ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty);
422-
debug!(?ty_const);
423-
424-
return Self::Ty(ty_const);
425-
}
426-
_ => {}
427-
}
428-
429-
let hir_id = tcx.hir().local_def_id_to_hir_id(def);
430-
let parent_args = if let Some(parent_hir_id) = tcx.hir().opt_parent_id(hir_id)
431-
&& let Some(parent_did) = parent_hir_id.as_owner()
432-
{
433-
GenericArgs::identity_for_item(tcx, parent_did)
434-
} else {
435-
List::empty()
436-
};
437-
debug!(?parent_args);
438-
439-
let did = def.to_def_id();
440-
let child_args = GenericArgs::identity_for_item(tcx, did);
441-
let args = tcx.mk_args_from_iter(parent_args.into_iter().chain(child_args.into_iter()));
442-
debug!(?args);
443-
444-
let span = tcx.def_span(def);
445-
let uneval = UnevaluatedConst::new(did, args);
446-
debug!(?span, ?param_env);
447-
448-
match tcx.const_eval_resolve(param_env, uneval, Some(span)) {
449-
Ok(val) => {
450-
debug!("evaluated const value");
451-
Self::Val(val, ty)
452-
}
453-
Err(_) => {
454-
debug!("error encountered during evaluation");
455-
// Error was handled in `const_eval_resolve`. Here we just create a
456-
// new unevaluated const and error hard later in codegen
457-
Self::Unevaluated(
458-
UnevaluatedConst {
459-
def: did,
460-
args: GenericArgs::identity_for_item(tcx, did),
461-
promoted: None,
462-
},
463-
ty,
464-
)
465-
}
466-
}
467-
}
468-
469373
pub fn from_ty_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
470374
match c.kind() {
471375
ty::ConstKind::Value(valtree) => {

compiler/rustc_middle/src/ty/consts.rs

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ impl<'tcx> Const<'tcx> {
207207
}
208208
}
209209

210+
// FIXME(const_generics): We currently have to special case parameters because `min_const_generics`
211+
// does not provide the parents generics to anonymous constants. We still allow generic const
212+
// parameters by themselves however, e.g. `N`. These constants would cause an ICE if we were to
213+
// ever try to substitute the generic parameters in their bodies.
210214
match expr.kind {
211215
hir::ExprKind::Path(hir::QPath::Resolved(
212216
_,

compiler/rustc_mir_build/src/thir/cx/expr.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -647,15 +647,33 @@ impl<'tcx> Cx<'tcx> {
647647
out_expr: out_expr.map(|expr| self.mirror_expr(expr)),
648648
},
649649
hir::InlineAsmOperand::Const { ref anon_const } => {
650-
let value =
651-
mir::Const::from_anon_const(tcx, anon_const.def_id, self.param_env);
650+
let value = mir::Const::Unevaluated(
651+
mir::UnevaluatedConst {
652+
def: anon_const.def_id.to_def_id(),
653+
args: GenericArgs::identity_for_item(
654+
self.tcx,
655+
anon_const.def_id,
656+
),
657+
promoted: None,
658+
},
659+
tcx.type_of(anon_const.def_id).instantiate_identity(),
660+
);
652661
let span = tcx.def_span(anon_const.def_id);
653662

654663
InlineAsmOperand::Const { value, span }
655664
}
656665
hir::InlineAsmOperand::SymFn { ref anon_const } => {
657-
let value =
658-
mir::Const::from_anon_const(tcx, anon_const.def_id, self.param_env);
666+
let value = mir::Const::Unevaluated(
667+
mir::UnevaluatedConst {
668+
def: anon_const.def_id.to_def_id(),
669+
args: GenericArgs::identity_for_item(
670+
self.tcx,
671+
anon_const.def_id,
672+
),
673+
promoted: None,
674+
},
675+
tcx.type_of(anon_const.def_id).instantiate_identity(),
676+
);
659677
let span = tcx.def_span(anon_const.def_id);
660678

661679
InlineAsmOperand::SymFn { value, span }

0 commit comments

Comments
 (0)