Skip to content

Commit d30f56d

Browse files
committed
Replace const_error methods with Const::new_error
1 parent ddbc774 commit d30f56d

File tree

15 files changed

+66
-59
lines changed

15 files changed

+66
-59
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,13 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
528528
let reported = err.emit();
529529
term = match def_kind {
530530
hir::def::DefKind::AssocTy => tcx.ty_error(reported).into(),
531-
hir::def::DefKind::AssocConst => tcx
532-
.const_error(
533-
tcx.type_of(assoc_item_def_id)
534-
.subst(tcx, projection_ty.skip_binder().substs),
535-
reported,
536-
)
537-
.into(),
531+
hir::def::DefKind::AssocConst => ty::Const::new_error(
532+
tcx,
533+
reported,
534+
tcx.type_of(assoc_item_def_id)
535+
.subst(tcx, projection_ty.skip_binder().substs),
536+
)
537+
.into(),
538538
_ => unreachable!(),
539539
};
540540
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
482482
self.astconv.ct_infer(ty, Some(param), inf.span).into()
483483
} else {
484484
self.inferred_params.push(inf.span);
485-
tcx.const_error_misc(ty).into()
485+
ty::Const::new_misc_error(tcx, ty).into()
486486
}
487487
}
488488
_ => unreachable!(),
@@ -537,7 +537,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
537537
.no_bound_vars()
538538
.expect("const parameter types cannot be generic");
539539
if let Err(guar) = ty.error_reported() {
540-
return tcx.const_error(ty, guar).into();
540+
return ty::Const::new_error(tcx, guar, ty).into();
541541
}
542542
if !infer_args && has_default {
543543
tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
@@ -546,7 +546,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
546546
self.astconv.ct_infer(ty, Some(param), self.span).into()
547547
} else {
548548
// We've already errored above about the mismatch.
549-
tcx.const_error_misc(ty).into()
549+
ty::Const::new_misc_error(tcx, ty).into()
550550
}
551551
}
552552
}

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
390390
// left alone.
391391
r => bug!("unexpected region: {r:?}"),
392392
});
393-
self.tcx().const_error_with_message(ty, span, "bad placeholder constant")
393+
ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant")
394394
}
395395

396396
fn projected_ty_from_poly_trait_ref(

compiler/rustc_hir_typeck/src/writeback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Resolver<'cx, 'tcx> {
840840
debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct);
841841
let e = self.report_error(ct);
842842
self.replaced_with_error = Some(e);
843-
self.fcx.tcx.const_error(ct.ty(), e)
843+
ty::Const::new_error(self.fcx.tcx, e, ct.ty())
844844
}
845845
}
846846
}

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ impl<'tcx> InferCtxt<'tcx> {
189189
// HACK: equating both sides with `[const error]` eagerly prevents us
190190
// from leaving unconstrained inference vars during things like impl
191191
// matching in the solver.
192-
let a_error = self.tcx.const_error(a.ty(), guar);
192+
let a_error = ty::Const::new_error(self.tcx, guar, a.ty());
193193
if let ty::ConstKind::Infer(InferConst::Var(vid)) = a.kind() {
194194
return self.unify_const_variable(vid, a_error, relation.param_env());
195195
}
196-
let b_error = self.tcx.const_error(b.ty(), guar);
196+
let b_error = ty::Const::new_error(self.tcx, guar, b.ty());
197197
if let ty::ConstKind::Infer(InferConst::Var(vid)) = b.kind() {
198198
return self.unify_const_variable(vid, b_error, relation.param_env());
199199
}

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ impl<'tcx> ConstantKind<'tcx> {
23322332
if let Some(val) = c.kind().try_eval_for_mir(tcx, param_env) {
23332333
match val {
23342334
Ok(val) => Self::Val(val, c.ty()),
2335-
Err(guar) => Self::Ty(tcx.const_error(self.ty(), guar)),
2335+
Err(guar) => Self::Ty(ty::Const::new_error(tcx, guar, self.ty())),
23362336
}
23372337
} else {
23382338
self
@@ -2344,7 +2344,9 @@ impl<'tcx> ConstantKind<'tcx> {
23442344
match tcx.const_eval_resolve(param_env, uneval, None) {
23452345
Ok(val) => Self::Val(val, ty),
23462346
Err(ErrorHandled::TooGeneric) => self,
2347-
Err(ErrorHandled::Reported(guar)) => Self::Ty(tcx.const_error(ty, guar.into())),
2347+
Err(ErrorHandled::Reported(guar)) => {
2348+
Self::Ty(ty::Const::new_error(tcx, guar.into(), ty))
2349+
}
23482350
}
23492351
}
23502352
}

compiler/rustc_middle/src/ty/abstract_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> TyCtxt<'tcx> {
5353
fn fold_const(&mut self, c: Const<'tcx>) -> Const<'tcx> {
5454
let ct = match c.kind() {
5555
ty::ConstKind::Unevaluated(uv) => match self.tcx.thir_abstract_const(uv.def) {
56-
Err(e) => self.tcx.const_error(c.ty(), e),
56+
Err(e) => ty::Const::new_error(self.tcx, e, c.ty()),
5757
Ok(Some(bac)) => {
5858
let substs = self.tcx.erase_regions(uv.substs);
5959
let bac = bac.subst(self.tcx, substs);

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::middle::resolve_bound_vars as rbv;
22
use crate::mir::interpret::LitToConstInput;
33
use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt};
44
use rustc_data_structures::intern::Interned;
5+
use rustc_error_messages::MultiSpan;
56
use rustc_hir as hir;
67
use rustc_hir::def::{DefKind, Res};
78
use rustc_hir::def_id::LocalDefId;
@@ -13,6 +14,7 @@ mod valtree;
1314

1415
pub use int::*;
1516
pub use kind::*;
17+
use rustc_span::DUMMY_SP;
1618
pub use valtree::*;
1719

1820
/// Use this rather than `ConstData`, whenever possible.
@@ -104,6 +106,34 @@ impl<'tcx> Const<'tcx> {
104106
Const::new(tcx, ty::ConstKind::Expr(expr), ty)
105107
}
106108

109+
#[inline]
110+
pub fn new_error(tcx: TyCtxt<'tcx>, e: ty::ErrorGuaranteed, ty: Ty<'tcx>) -> Const<'tcx> {
111+
Const::new(tcx, ty::ConstKind::Error(e), ty)
112+
}
113+
114+
/// Like [TyCtxt::ty_error] but for constants.
115+
#[track_caller]
116+
pub fn new_misc_error(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
117+
Const::new_error_with_message(
118+
tcx,
119+
ty,
120+
DUMMY_SP,
121+
"ty::ConstKind::Error constructed but no error reported",
122+
)
123+
}
124+
125+
/// Like [TyCtxt::ty_error_with_message] but for constants.
126+
#[track_caller]
127+
pub fn new_error_with_message<S: Into<MultiSpan>>(
128+
tcx: TyCtxt<'tcx>,
129+
ty: Ty<'tcx>,
130+
span: S,
131+
msg: &'static str,
132+
) -> Const<'tcx> {
133+
let reported = tcx.sess.delay_span_bug(span, msg);
134+
Const::new_error(tcx, reported, ty)
135+
}
136+
107137
/// Literals and const generic parameters are eagerly converted to a constant, everything else
108138
/// becomes `Unevaluated`.
109139
#[instrument(skip(tcx), level = "debug")]
@@ -200,7 +230,9 @@ impl<'tcx> Const<'tcx> {
200230
param_ty,
201231
))
202232
}
203-
Some(rbv::ResolvedArg::Error(guar)) => Some(tcx.const_error(param_ty, guar)),
233+
Some(rbv::ResolvedArg::Error(guar)) => {
234+
Some(ty::Const::new_error(tcx, guar, param_ty))
235+
}
204236
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", expr.hir_id),
205237
}
206238
}
@@ -285,7 +317,7 @@ impl<'tcx> Const<'tcx> {
285317
if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
286318
match val {
287319
Ok(val) => ty::Const::new_value(tcx, val, self.ty()),
288-
Err(guar) => tcx.const_error(self.ty(), guar),
320+
Err(guar) => ty::Const::new_error(tcx, guar, self.ty()),
289321
}
290322
} else {
291323
// Either the constant isn't evaluatable or ValTree creation failed.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -733,34 +733,6 @@ impl<'tcx> TyCtxt<'tcx> {
733733
self.mk_ty_from_kind(Error(reported))
734734
}
735735

736-
/// Like [TyCtxt::ty_error] but for constants, with current `ErrorGuaranteed`
737-
#[track_caller]
738-
pub fn const_error(self, ty: Ty<'tcx>, reported: ErrorGuaranteed) -> Const<'tcx> {
739-
self.mk_ct_from_kind(ty::ConstKind::Error(reported), ty)
740-
}
741-
742-
/// Like [TyCtxt::ty_error] but for constants.
743-
#[track_caller]
744-
pub fn const_error_misc(self, ty: Ty<'tcx>) -> Const<'tcx> {
745-
self.const_error_with_message(
746-
ty,
747-
DUMMY_SP,
748-
"ty::ConstKind::Error constructed but no error reported",
749-
)
750-
}
751-
752-
/// Like [TyCtxt::ty_error_with_message] but for constants.
753-
#[track_caller]
754-
pub fn const_error_with_message<S: Into<MultiSpan>>(
755-
self,
756-
ty: Ty<'tcx>,
757-
span: S,
758-
msg: &'static str,
759-
) -> Const<'tcx> {
760-
let reported = self.sess.delay_span_bug(span, msg);
761-
self.const_error(ty, reported)
762-
}
763-
764736
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {
765737
self.sess.consider_optimizing(|| self.crate_name(LOCAL_CRATE), msg)
766738
}

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ impl GenericParamDef {
102102
match &self.kind {
103103
ty::GenericParamDefKind::Lifetime => ty::Region::new_error_misc(tcx).into(),
104104
ty::GenericParamDefKind::Type { .. } => tcx.ty_error_misc().into(),
105-
ty::GenericParamDefKind::Const { .. } => {
106-
tcx.const_error_misc(tcx.type_of(self.def_id).subst(tcx, preceding_substs)).into()
107-
}
105+
ty::GenericParamDefKind::Const { .. } => ty::Const::new_misc_error(
106+
tcx,
107+
tcx.type_of(self.def_id).subst(tcx, preceding_substs),
108+
)
109+
.into(),
108110
}
109111
}
110112
}

compiler/rustc_middle/src/ty/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> {
216216
})
217217
.emit_unless(self.ignore_errors);
218218

219-
self.interner().const_error(ct.ty(), guar)
219+
ty::Const::new_error(self.tcx, guar, ct.ty())
220220
}
221221
}
222222
}

compiler/rustc_mir_build/src/build/expr/as_constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn as_constant_inner<'tcx>(
5252
match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
5353
Ok(c) => c,
5454
Err(LitToConstError::Reported(guar)) => {
55-
ConstantKind::Ty(tcx.const_error(ty, guar))
55+
ConstantKind::Ty(ty::Const::new_error(tcx, guar, ty))
5656
}
5757
Err(LitToConstError::TypeError) => {
5858
bug!("encountered type error in `lit_to_mir_constant`")

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
915915
use rustc_middle::mir::interpret::ErrorHandled;
916916
match self.infcx.try_const_eval_resolve(param_env, unevaluated, ty, None) {
917917
Ok(ct) => Some(ct),
918-
Err(ErrorHandled::Reported(e)) => Some(self.tcx().const_error(ty, e.into())),
918+
Err(ErrorHandled::Reported(e)) => Some(ty::Const::new_error(self.tcx(), e.into(), ty)),
919919
Err(ErrorHandled::TooGeneric) => None,
920920
}
921921
}

compiler/rustc_trait_selection/src/solve/project_goals.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,10 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
186186
"missing value for assoc item in impl",
187187
);
188188
let error_term = match assoc_def.item.kind {
189-
ty::AssocKind::Const => tcx
190-
.const_error(
191-
tcx.type_of(goal.predicate.def_id())
192-
.subst(tcx, goal.predicate.projection_ty.substs),
193-
guar,
189+
ty::AssocKind::Const => ty::Const::new_error(tcx,
190+
guar,
191+
tcx.type_of(goal.predicate.def_id())
192+
.subst(tcx, goal.predicate.projection_ty.substs),
194193
)
195194
.into(),
196195
ty::AssocKind::Type => tcx.ty_error(guar).into(),

compiler/rustc_ty_utils/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn recurse_build<'tcx>(
119119
let sp = node.span;
120120
match tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
121121
Ok(c) => c,
122-
Err(LitToConstError::Reported(guar)) => tcx.const_error(node.ty, guar),
122+
Err(LitToConstError::Reported(guar)) => ty::Const::new_error(tcx, guar, node.ty),
123123
Err(LitToConstError::TypeError) => {
124124
bug!("encountered type error in lit_to_const")
125125
}

0 commit comments

Comments
 (0)