Skip to content

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

+4-1
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

+1-1
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

+3-3
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

+2-8
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

+1-1
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

+2-2
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

+1
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

+12
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

+8-6
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

+4
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")]

src/librustc_middle/ty/walk.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
170170
| ty::ConstKind::Param(_)
171171
| ty::ConstKind::Placeholder(_)
172172
| ty::ConstKind::Bound(..)
173-
| ty::ConstKind::Value(_) => {}
173+
| ty::ConstKind::Value(_)
174+
| ty::ConstKind::Error => {}
174175

175176
ty::ConstKind::Unevaluated(_, substs, _) => {
176177
stack.extend(substs.iter().copied().rev());

src/librustc_mir/interpret/operand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
518518
// Early-return cases.
519519
let val_val = match val.val {
520520
ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
521+
ty::ConstKind::Error => throw_inval!(TypeckError),
521522
ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
522523
let instance = self.resolve(def_id, substs)?;
523524
// We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.

src/librustc_trait_selection/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
972972
)
973973
.emit();
974974

975-
self.tcx().consts.err
975+
self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty: ct.ty })
976976
}
977977
}
978978
}

src/librustc_typeck/astconv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -826,14 +826,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
826826
}
827827
}
828828
GenericParamDefKind::Const => {
829+
let ty = tcx.at(span).type_of(param.def_id);
829830
// FIXME(const_generics:defaults)
830831
if infer_args {
831832
// No const parameters were provided, we can infer all.
832-
let ty = tcx.at(span).type_of(param.def_id);
833833
self.ct_infer(ty, Some(param), span).into()
834834
} else {
835835
// We've already errored above about the mismatch.
836-
tcx.consts.err.into()
836+
tcx.mk_const(ty::Const { val: ty::ConstKind::Error, ty }).into()
837837
}
838838
}
839839
}

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
673673
// FIXME: we'd like to use `self.report_error`, but it doesn't yet
674674
// accept a &'tcx ty::Const.
675675
self.replaced_with_error = true;
676-
self.tcx().consts.err
676+
self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty: ct.ty })
677677
}
678678
}
679679
}

src/librustc_typeck/collect.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
316316

317317
fn ct_infer(
318318
&self,
319-
_: Ty<'tcx>,
319+
ty: Ty<'tcx>,
320320
_: Option<&ty::GenericParamDef>,
321321
span: Span,
322322
) -> &'tcx Const<'tcx> {
323323
bad_placeholder_type(self.tcx(), vec![span]).emit();
324324

325-
self.tcx().consts.err
325+
self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty })
326326
}
327327

328328
fn projected_ty_from_poly_trait_ref(
@@ -2037,7 +2037,8 @@ fn associated_item_predicates(
20372037
}
20382038
ty::GenericParamDefKind::Const => {
20392039
unimplemented_error("const");
2040-
tcx.consts.err.into()
2040+
tcx.mk_const(ty::Const { val: ty::ConstKind::Error, ty: tcx.type_of(param.def_id) })
2041+
.into()
20412042
}
20422043
}
20432044
};

0 commit comments

Comments
 (0)