1
Fork 0

Use error constant instead of explicit error handling

This commit is contained in:
Oli Scherer 2025-01-07 12:03:28 +00:00
parent 7833cf7a47
commit 787af97bab
5 changed files with 3 additions and 10 deletions

View file

@ -2456,9 +2456,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
LitToConstInput { lit: &lit.node, ty, neg: negated }; LitToConstInput { lit: &lit.node, ty, neg: negated };
let ct = match tcx.lit_to_const(lit_input) { let ct = match tcx.lit_to_const(lit_input) {
Ok(c) => c, Ok(c) => c,
Err(LitToConstError::Reported(err)) => {
ty::Const::new_error(tcx, err)
}
Err(LitToConstError::TypeError) => todo!(), Err(LitToConstError::TypeError) => todo!(),
}; };
(ct, ty) (ct, ty)

View file

@ -16,7 +16,6 @@ use rustc_abi::{AddressSpace, Align, Endian, HasDataLayout, Size};
use rustc_ast::{LitKind, Mutability}; use rustc_ast::{LitKind, Mutability};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
@ -91,7 +90,6 @@ pub enum LitToConstError {
/// This is used for graceful error handling (`span_delayed_bug`) in /// This is used for graceful error handling (`span_delayed_bug`) in
/// type checking (`Const::from_anon_const`). /// type checking (`Const::from_anon_const`).
TypeError, TypeError,
Reported(ErrorGuaranteed),
} }
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]

View file

@ -62,13 +62,13 @@ pub(crate) fn lit_to_const<'tcx>(
} }
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()), (ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
(ast::LitKind::Float(n, _), ty::Float(fty)) => { (ast::LitKind::Float(n, _), ty::Float(fty)) => {
let bits = parse_float_into_scalar(*n, *fty, neg).ok_or_else(|| { let bits = parse_float_into_scalar(*n, *fty, neg).unwrap_or_else(|| {
tcx.dcx().bug(format!("couldn't parse float literal: {:?}", lit_input.lit)) tcx.dcx().bug(format!("couldn't parse float literal: {:?}", lit_input.lit))
})?; });
ty::ValTree::from_scalar_int(bits) ty::ValTree::from_scalar_int(bits)
} }
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()), (ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
(ast::LitKind::Err(guar), _) => return Err(LitToConstError::Reported(*guar)), (ast::LitKind::Err(guar), _) => return Ok(ty::Const::new_error(tcx, *guar)),
_ => return Err(LitToConstError::TypeError), _ => return Err(LitToConstError::TypeError),
}; };

View file

@ -671,7 +671,6 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let lit_input = LitToConstInput { lit: &lit.node, ty: ct_ty, neg }; let lit_input = LitToConstInput { lit: &lit.node, ty: ct_ty, neg };
match self.tcx.at(expr.span).lit_to_const(lit_input) { match self.tcx.at(expr.span).lit_to_const(lit_input) {
Ok(constant) => self.const_to_pat(constant, ct_ty, expr.hir_id, lit.span).kind, Ok(constant) => self.const_to_pat(constant, ct_ty, expr.hir_id, lit.span).kind,
Err(LitToConstError::Reported(e)) => PatKind::Error(e),
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"), Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
} }
} }

View file

@ -120,7 +120,6 @@ fn recurse_build<'tcx>(
let sp = node.span; let sp = node.span;
match tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) { match tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
Ok(c) => c, Ok(c) => c,
Err(LitToConstError::Reported(guar)) => ty::Const::new_error(tcx, guar),
Err(LitToConstError::TypeError) => { Err(LitToConstError::TypeError) => {
bug!("encountered type error in lit_to_const") bug!("encountered type error in lit_to_const")
} }