Avoid various uses of Option<Span> in favor of using DUMMY_SP in the few cases that used None

This commit is contained in:
Oli Scherer
2024-03-14 09:10:28 +00:00
parent a42873e85b
commit adda9da604
34 changed files with 70 additions and 75 deletions

View File

@@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Display, Formatter};
use rustc_hir::def_id::DefId;
use rustc_session::RemapFileNameExt;
use rustc_span::Span;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::{HasDataLayout, Size};
use crate::mir::interpret::{alloc_range, AllocId, ConstAllocation, ErrorHandled, Scalar};
@@ -273,7 +273,7 @@ impl<'tcx> Const<'tcx> {
self,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
span: Option<Span>,
span: Span,
) -> Result<ConstValue<'tcx>, ErrorHandled> {
match self {
Const::Ty(c) => {
@@ -293,7 +293,7 @@ impl<'tcx> Const<'tcx> {
/// Normalizes the constant to a value or an error if possible.
#[inline]
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
match self.eval(tcx, param_env, None) {
match self.eval(tcx, param_env, DUMMY_SP) {
Ok(val) => Self::Val(val, self.ty()),
Err(ErrorHandled::Reported(guar, _span)) => {
Self::Ty(ty::Const::new_error(tcx, guar.into(), self.ty()))
@@ -313,10 +313,10 @@ impl<'tcx> Const<'tcx> {
// Avoid the `valtree_to_const_val` query. Can only be done on primitive types that
// are valtree leaves, and *not* on references. (References should return the
// pointer here, which valtrees don't represent.)
let val = c.eval(tcx, param_env, None).ok()?;
let val = c.eval(tcx, param_env, DUMMY_SP).ok()?;
Some(val.unwrap_leaf().into())
}
_ => self.eval(tcx, param_env, None).ok()?.try_to_scalar(),
_ => self.eval(tcx, param_env, DUMMY_SP).ok()?.try_to_scalar(),
}
}

View File

@@ -24,7 +24,7 @@ impl<'tcx> TyCtxt<'tcx> {
let instance = ty::Instance::new(def_id, args);
let cid = GlobalId { instance, promoted: None };
let param_env = self.param_env(def_id).with_reveal_all_normalized(self);
self.const_eval_global_id(param_env, cid, None)
self.const_eval_global_id(param_env, cid, DUMMY_SP)
}
/// Resolves and evaluates a constant.
///
@@ -40,7 +40,7 @@ impl<'tcx> TyCtxt<'tcx> {
self,
param_env: ty::ParamEnv<'tcx>,
ct: mir::UnevaluatedConst<'tcx>,
span: Option<Span>,
span: Span,
) -> EvalToConstValueResult<'tcx> {
// Cannot resolve `Unevaluated` constants that contain inference
// variables. We reject those here since `resolve`
@@ -73,7 +73,7 @@ impl<'tcx> TyCtxt<'tcx> {
self,
param_env: ty::ParamEnv<'tcx>,
ct: ty::UnevaluatedConst<'tcx>,
span: Option<Span>,
span: Span,
) -> EvalToValTreeResult<'tcx> {
// Cannot resolve `Unevaluated` constants that contain inference
// variables. We reject those here since `resolve`
@@ -130,7 +130,7 @@ impl<'tcx> TyCtxt<'tcx> {
self,
param_env: ty::ParamEnv<'tcx>,
instance: ty::Instance<'tcx>,
span: Option<Span>,
span: Span,
) -> EvalToConstValueResult<'tcx> {
self.const_eval_global_id(param_env, GlobalId { instance, promoted: None }, span)
}
@@ -141,12 +141,12 @@ impl<'tcx> TyCtxt<'tcx> {
self,
param_env: ty::ParamEnv<'tcx>,
cid: GlobalId<'tcx>,
span: Option<Span>,
span: Span,
) -> EvalToConstValueResult<'tcx> {
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
// improve caching of queries.
let inputs = self.erase_regions(param_env.with_reveal_all_normalized(self).and(cid));
if let Some(span) = span {
if !span.is_dummy() {
// The query doesn't know where it is being invoked, so we need to fix the span.
self.at(span).eval_to_const_value_raw(inputs).map_err(|e| e.with_span(span))
} else {
@@ -160,13 +160,13 @@ impl<'tcx> TyCtxt<'tcx> {
self,
param_env: ty::ParamEnv<'tcx>,
cid: GlobalId<'tcx>,
span: Option<Span>,
span: Span,
) -> EvalToValTreeResult<'tcx> {
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
// improve caching of queries.
let inputs = self.erase_regions(param_env.with_reveal_all_normalized(self).and(cid));
debug!(?inputs);
if let Some(span) = span {
if !span.is_dummy() {
// The query doesn't know where it is being invoked, so we need to fix the span.
self.at(span).eval_to_valtree(inputs).map_err(|e| e.with_span(span))
} else {

View File

@@ -335,7 +335,7 @@ impl<'tcx> Const<'tcx> {
self,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
span: Option<Span>,
span: Span,
) -> Result<ValTree<'tcx>, ErrorHandled> {
assert!(!self.has_escaping_bound_vars(), "escaping vars in {self:?}");
match self.kind() {
@@ -349,7 +349,7 @@ impl<'tcx> Const<'tcx> {
else {
// This can happen when we run on ill-typed code.
let e = tcx.dcx().span_delayed_bug(
span.unwrap_or(DUMMY_SP),
span,
"`ty::Const::eval` called on a non-valtree-compatible type",
);
return Err(e.into());
@@ -362,14 +362,14 @@ impl<'tcx> Const<'tcx> {
| ConstKind::Infer(_)
| ConstKind::Bound(_, _)
| ConstKind::Placeholder(_)
| ConstKind::Expr(_) => Err(ErrorHandled::TooGeneric(span.unwrap_or(DUMMY_SP))),
| ConstKind::Expr(_) => Err(ErrorHandled::TooGeneric(span)),
}
}
/// Normalizes the constant to a value or an error if possible.
#[inline]
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Self {
match self.eval(tcx, param_env, None) {
match self.eval(tcx, param_env, DUMMY_SP) {
Ok(val) => Self::new_value(tcx, val, self.ty()),
Err(ErrorHandled::Reported(r, _span)) => Self::new_error(tcx, r.into(), self.ty()),
Err(ErrorHandled::TooGeneric(_span)) => self,
@@ -382,7 +382,7 @@ impl<'tcx> Const<'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<Scalar> {
self.eval(tcx, param_env, None).ok()?.try_to_scalar()
self.eval(tcx, param_env, DUMMY_SP).ok()?.try_to_scalar()
}
#[inline]