Skip to content

Commit 2256627

Browse files
committed
Remove the now-useless Result from lit_to_const
1 parent 56dd382 commit 2256627

File tree

7 files changed

+19
-47
lines changed

7 files changed

+19
-47
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
3535
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3636
use rustc_infer::traits::ObligationCause;
3737
use rustc_middle::middle::stability::AllowUnstable;
38-
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
38+
use rustc_middle::mir::interpret::{LitToConstInput};
3939
use rustc_middle::ty::fold::fold_regions;
4040
use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
4141
use rustc_middle::ty::{
@@ -2269,7 +2269,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22692269
// Allow the `ty` to be an alias type, though we cannot handle it here, we just go through
22702270
// the more expensive anon const code path.
22712271
if !lit_input.ty.has_aliases() {
2272-
return Some(tcx.at(expr.span).lit_to_const(lit_input).unwrap());
2272+
return Some(tcx.at(expr.span).lit_to_const(lit_input));
22732273
}
22742274
}
22752275

@@ -2453,10 +2453,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24532453
hir::ExprKind::Lit(lit) => {
24542454
let lit_input =
24552455
LitToConstInput { lit: &lit.node, ty, neg: neg.is_some() };
2456-
let ct = match tcx.lit_to_const(lit_input) {
2457-
Ok(c) => c,
2458-
Err(LitToConstError::TypeError) => todo!(),
2459-
};
2456+
let ct = tcx.lit_to_const(lit_input);
24602457
(ct, ty)
24612458
}
24622459

compiler/rustc_middle/src/mir/interpret/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,6 @@ pub struct LitToConstInput<'tcx> {
8383
pub neg: bool,
8484
}
8585

86-
/// Error type for `tcx.lit_to_const`.
87-
#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)]
88-
pub enum LitToConstError {
89-
/// The literal's inferred type did not match the expected `ty` in the input.
90-
/// This is used for graceful error handling (`span_delayed_bug`) in
91-
/// type checking (`Const::from_anon_const`).
92-
TypeError,
93-
}
94-
9586
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
9687
pub struct AllocId(pub NonZero<u64>);
9788

compiler/rustc_middle/src/query/erase.rs

-9
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,6 @@ impl EraseType for Result<rustc_abi::TyAndLayout<'_, Ty<'_>>, &ty::layout::Layou
141141
>()];
142142
}
143143

144-
impl EraseType for Result<ty::Const<'_>, mir::interpret::LitToConstError> {
145-
type Result = [u8; size_of::<Result<ty::Const<'static>, mir::interpret::LitToConstError>>()];
146-
}
147-
148-
impl EraseType for Result<mir::Const<'_>, mir::interpret::LitToConstError> {
149-
type Result = [u8; size_of::<Result<mir::Const<'static>, mir::interpret::LitToConstError>>()];
150-
}
151-
152144
impl EraseType for Result<mir::ConstAlloc<'_>, mir::interpret::ErrorHandled> {
153145
type Result = [u8; size_of::<Result<mir::ConstAlloc<'static>, mir::interpret::ErrorHandled>>()];
154146
}
@@ -296,7 +288,6 @@ trivial! {
296288
rustc_middle::mir::interpret::AllocId,
297289
rustc_middle::mir::interpret::CtfeProvenance,
298290
rustc_middle::mir::interpret::ErrorHandled,
299-
rustc_middle::mir::interpret::LitToConstError,
300291
rustc_middle::thir::ExprId,
301292
rustc_middle::traits::CodegenObligationError,
302293
rustc_middle::traits::EvaluationResult,

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use crate::middle::resolve_bound_vars::{ObjectLifetimeDefault, ResolveBoundVars,
5757
use crate::middle::stability::{self, DeprecationEntry};
5858
use crate::mir::interpret::{
5959
EvalStaticInitializerRawResult, EvalToAllocationRawResult, EvalToConstValueResult,
60-
EvalToValTreeResult, GlobalId, LitToConstError, LitToConstInput,
60+
EvalToValTreeResult, GlobalId, LitToConstInput,
6161
};
6262
use crate::mir::mono::{CodegenUnit, CollectionMode, MonoItem};
6363
use crate::query::erase::{Erase, erase, restore};
@@ -1268,7 +1268,7 @@ rustc_queries! {
12681268
// FIXME get rid of this with valtrees
12691269
query lit_to_const(
12701270
key: LitToConstInput<'tcx>
1271-
) -> Result<ty::Const<'tcx>, LitToConstError> {
1271+
) -> ty::Const<'tcx> {
12721272
desc { "converting literal to const" }
12731273
}
12741274

compiler/rustc_mir_build/src/thir/constant.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast as ast;
22
use rustc_hir::LangItem;
33
use rustc_middle::bug;
4-
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
4+
use rustc_middle::mir::interpret::LitToConstInput;
55
use rustc_middle::ty::{self, ScalarInt, TyCtxt, TypeVisitableExt as _};
66
use tracing::trace;
77

@@ -10,11 +10,11 @@ use crate::builder::parse_float_into_scalar;
1010
pub(crate) fn lit_to_const<'tcx>(
1111
tcx: TyCtxt<'tcx>,
1212
lit_input: LitToConstInput<'tcx>,
13-
) -> Result<ty::Const<'tcx>, LitToConstError> {
13+
) -> ty::Const<'tcx> {
1414
let LitToConstInput { lit, ty, neg } = lit_input;
1515

1616
if let Err(guar) = ty.error_reported() {
17-
return Ok(ty::Const::new_error(tcx, guar));
17+
return ty::Const::new_error(tcx, guar);
1818
}
1919

2020
let trunc = |n| {
@@ -28,8 +28,8 @@ pub(crate) fn lit_to_const<'tcx>(
2828
let result = width.truncate(n);
2929
trace!("trunc result: {}", result);
3030

31-
Ok(ScalarInt::try_from_uint(result, width)
32-
.unwrap_or_else(|| bug!("expected to create ScalarInt from uint {:?}", result)))
31+
ScalarInt::try_from_uint(result, width)
32+
.unwrap_or_else(|| bug!("expected to create ScalarInt from uint {:?}", result))
3333
};
3434

3535
let valtree = match (lit, ty.kind()) {
@@ -57,7 +57,7 @@ pub(crate) fn lit_to_const<'tcx>(
5757
}
5858
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
5959
let scalar_int =
60-
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() })?;
60+
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() });
6161
ty::ValTree::from_scalar_int(scalar_int)
6262
}
6363
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
@@ -68,9 +68,9 @@ pub(crate) fn lit_to_const<'tcx>(
6868
ty::ValTree::from_scalar_int(bits)
6969
}
7070
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
71-
(ast::LitKind::Err(guar), _) => return Ok(ty::Const::new_error(tcx, *guar)),
72-
_ => return Ok(ty::Const::new_misc_error(tcx)),
71+
(ast::LitKind::Err(guar), _) => return ty::Const::new_error(tcx, *guar),
72+
_ => return ty::Const::new_misc_error(tcx),
7373
};
7474

75-
Ok(ty::Const::new_value(tcx, valtree, ty))
75+
ty::Const::new_value(tcx, valtree, ty)
7676
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1313
use rustc_hir::{self as hir, ByRef, Mutability, RangeEnd};
1414
use rustc_index::Idx;
1515
use rustc_lint as lint;
16-
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
16+
use rustc_middle::mir::interpret::LitToConstInput;
1717
use rustc_middle::thir::{
1818
Ascription, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
1919
};
@@ -679,10 +679,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
679679

680680
let ct_ty = self.typeck_results.expr_ty(expr);
681681
let lit_input = LitToConstInput { lit: &lit.node, ty: ct_ty, neg };
682-
match self.tcx.at(expr.span).lit_to_const(lit_input) {
683-
Ok(constant) => self.const_to_pat(constant, ct_ty, expr.hir_id, lit.span).kind,
684-
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
685-
}
682+
let constant = self.tcx.at(expr.span).lit_to_const(lit_input);
683+
self.const_to_pat(constant, ct_ty, expr.hir_id, lit.span).kind
686684
}
687685
}
688686

compiler/rustc_ty_utils/src/consts.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_abi::{FIRST_VARIANT, VariantIdx};
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_hir::def::DefKind;
66
use rustc_hir::def_id::LocalDefId;
7-
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
7+
use rustc_middle::mir::interpret::LitToConstInput;
88
use rustc_middle::query::Providers;
99
use rustc_middle::thir::visit;
1010
use rustc_middle::thir::visit::Visitor;
@@ -118,12 +118,7 @@ fn recurse_build<'tcx>(
118118
}
119119
&ExprKind::Literal { lit, neg } => {
120120
let sp = node.span;
121-
match tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
122-
Ok(c) => c,
123-
Err(LitToConstError::TypeError) => {
124-
bug!("encountered type error in lit_to_const")
125-
}
126-
}
121+
tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg })
127122
}
128123
&ExprKind::NonHirLiteral { lit, user_ty: _ } => {
129124
let val = ty::ValTree::from_scalar_int(lit);

0 commit comments

Comments
 (0)