|
1 | 1 | use std::fmt::{self, Debug, Display, Formatter}; |
2 | 2 |
|
3 | 3 | 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; |
6 | 5 | use rustc_span::Span; |
7 | 6 | use rustc_target::abi::{HasDataLayout, Size}; |
8 | 7 |
|
9 | 8 | use crate::mir::interpret::{alloc_range, AllocId, ConstAllocation, ErrorHandled, Scalar}; |
10 | 9 | use crate::mir::{pretty_print_const_value, Promoted}; |
| 10 | +use crate::ty::GenericArgsRef; |
11 | 11 | 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}; |
14 | 13 |
|
15 | 14 | /////////////////////////////////////////////////////////////////////////// |
16 | 15 | /// Evaluated Constants |
@@ -371,101 +370,6 @@ impl<'tcx> Const<'tcx> { |
371 | 370 | Self::Val(val, ty) |
372 | 371 | } |
373 | 372 |
|
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 | | - |
469 | 373 | pub fn from_ty_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self { |
470 | 374 | match c.kind() { |
471 | 375 | ty::ConstKind::Value(valtree) => { |
|
0 commit comments