Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2deb39d

Browse files
committed
ty: add ty::ConstKind::Error to replace tcx.consts.err.
1 parent 4e4d49d commit 2deb39d

File tree

16 files changed

+49
-30
lines changed

16 files changed

+49
-30
lines changed

src/librustc_infer/infer/freshen.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
251251
bug!("unexpected const {:?}", ct)
252252
}
253253

254-
ty::ConstKind::Param(_) | ty::ConstKind::Value(_) | ty::ConstKind::Unevaluated(..) => {}
254+
ty::ConstKind::Param(_)
255+
| ty::ConstKind::Value(_)
256+
| ty::ConstKind::Unevaluated(..)
257+
| ty::ConstKind::Error => {}
255258
}
256259

257260
ct.super_fold_with(self)

src/librustc_infer/infer/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
227227
match c.val {
228228
ty::ConstKind::Infer(InferConst::Var(vid)) => {
229229
self.err = Some(FixupError::UnresolvedConst(vid));
230-
return self.tcx().consts.err;
230+
return self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty: c.ty });
231231
}
232232
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
233233
bug!("Unexpected const in full const resolver: {:?}", c);

src/librustc_middle/ty/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub struct CommonLifetimes<'tcx> {
182182
}
183183

184184
pub struct CommonConsts<'tcx> {
185-
pub err: &'tcx Const<'tcx>,
185+
pub unit: &'tcx Const<'tcx>,
186186
}
187187

188188
pub struct LocalTableInContext<'a, V> {
@@ -858,9 +858,9 @@ impl<'tcx> CommonConsts<'tcx> {
858858
let mk_const = |c| interners.const_.intern(c, |c| Interned(interners.arena.alloc(c))).0;
859859

860860
CommonConsts {
861-
err: mk_const(ty::Const {
861+
unit: mk_const(ty::Const {
862862
val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::zst())),
863-
ty: types.err,
863+
ty: types.unit,
864864
}),
865865
}
866866
}

src/librustc_middle/ty/flags.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,7 @@ impl FlagComputation {
7070
| &ty::Str
7171
| &ty::Foreign(..) => {}
7272

73-
// You might think that we could just return Error for
74-
// any type containing Error as a component, and get
75-
// rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
76-
// the exception of function types that return bot).
77-
// But doing so caused sporadic memory corruption, and
78-
// neither I (tjc) nor nmatsakis could figure out why,
79-
// so we're doing it this way.
80-
&ty::Error => self.add_flags(TypeFlags::HAS_TY_ERR),
73+
&ty::Error => self.add_flags(TypeFlags::HAS_ERROR),
8174

8275
&ty::Param(_) => {
8376
self.add_flags(TypeFlags::HAS_TY_PARAM);
@@ -239,6 +232,7 @@ impl FlagComputation {
239232
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
240233
}
241234
ty::ConstKind::Value(_) => {}
235+
ty::ConstKind::Error => self.add_flags(TypeFlags::HAS_ERROR),
242236
}
243237
}
244238

src/librustc_middle/ty/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
8282
self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
8383
}
8484
fn references_error(&self) -> bool {
85-
self.has_type_flags(TypeFlags::HAS_TY_ERR)
85+
self.has_type_flags(TypeFlags::HAS_ERROR)
8686
}
8787
fn has_param_types_or_consts(&self) -> bool {
8888
self.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_CT_PARAM)

src/librustc_middle/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ bitflags! {
567567
| TypeFlags::HAS_TY_OPAQUE.bits
568568
| TypeFlags::HAS_CT_PROJECTION.bits;
569569

570-
/// Is an error type reachable?
571-
const HAS_TY_ERR = 1 << 13;
570+
/// Is an error type/const reachable?
571+
const HAS_ERROR = 1 << 13;
572572

573573
/// Does this have any region that "appears free" in the type?
574574
/// Basically anything but [ReLateBound] and [ReErased].

src/librustc_middle/ty/print/pretty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ pub trait PrettyPrinter<'tcx>:
938938
self.pretty_print_bound_var(debruijn, bound_var)?
939939
}
940940
ty::ConstKind::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)),
941+
ty::ConstKind::Error => p!(write("[const error]")),
941942
};
942943
Ok(self)
943944
}

src/librustc_middle/ty/relate.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,21 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
510510
let tcx = relation.tcx();
511511

512512
let eagerly_eval = |x: &'tcx ty::Const<'tcx>| {
513+
// FIXME(eddyb) this doesn't account for lifetime inference variables
514+
// being erased by `eval`, *nor* for the polymorphic aspect of `eval`.
515+
// That is, we could always use `eval` and it will just return the
516+
// old value back if it doesn't succeed.
513517
if !x.val.needs_infer() {
514518
return x.eval(tcx, relation.param_env()).val;
515519
}
516520
x.val
517521
};
518522

523+
// FIXME(eddyb) doesn't look like everything below checks that `a.ty == b.ty`.
524+
// We could probably always assert it early, as `const` generic parameters
525+
// are not allowed to depend on other generic parameters, i.e. are concrete.
526+
// (although there could be normalization differences)
527+
519528
// Currently, the values that can be unified are primitive types,
520529
// and those that derive both `PartialEq` and `Eq`, corresponding
521530
// to structural-match types.
@@ -524,6 +533,9 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
524533
// The caller should handle these cases!
525534
bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b)
526535
}
536+
537+
(ty::ConstKind::Error, _) | (_, ty::ConstKind::Error) => Ok(ty::ConstKind::Error),
538+
527539
(ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) if a_p.index == b_p.index => {
528540
return Ok(a);
529541
}

src/librustc_middle/ty/structural_impls.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,10 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
10221022
ty::ConstKind::Unevaluated(did, substs, promoted) => {
10231023
ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
10241024
}
1025-
ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(..) => {
1026-
*self
1027-
}
1025+
ty::ConstKind::Value(_)
1026+
| ty::ConstKind::Bound(..)
1027+
| ty::ConstKind::Placeholder(..)
1028+
| ty::ConstKind::Error => *self,
10281029
}
10291030
}
10301031

@@ -1033,9 +1034,10 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
10331034
ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
10341035
ty::ConstKind::Param(p) => p.visit_with(visitor),
10351036
ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1036-
ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
1037-
false
1038-
}
1037+
ty::ConstKind::Value(_)
1038+
| ty::ConstKind::Bound(..)
1039+
| ty::ConstKind::Placeholder(_)
1040+
| ty::ConstKind::Error => false,
10391041
}
10401042
}
10411043
}

src/librustc_middle/ty/sty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,10 @@ pub enum ConstKind<'tcx> {
24292429

24302430
/// Used to hold computed value.
24312431
Value(ConstValue<'tcx>),
2432+
2433+
/// A placeholder for a const which could not be computed; this is
2434+
/// propagated to avoid useless error messages.
2435+
Error,
24322436
}
24332437

24342438
#[cfg(target_arch = "x86_64")]

0 commit comments

Comments
 (0)