Skip to content

Commit 1f84156

Browse files
committed
Auto merge of #58537 - pietroalbini:beta-backports, r=pietroalbini
[beta] Rollup backports Cherry-picked: * #58207: Make `intern_lazy_const` actually intern its argument. * #58161: Lower constant patterns with ascribed types. * #57908: resolve: Fix span arithmetics in the import conflict error * #57835: typeck: remove leaky nested probe during trait object method resolution * #57885: Avoid committing to autoderef in object method probing * #57646: Fixes text becoming invisible when element targetted Rolled up: * #58522: [BETA] Update cargo r? @ghost
2 parents 08f1073 + bbe9e21 commit 1f84156

33 files changed

+632
-112
lines changed

src/librustc/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ impl<'tcx> Operand<'tcx> {
21542154
span,
21552155
ty,
21562156
user_ty: None,
2157-
literal: tcx.intern_lazy_const(
2157+
literal: tcx.mk_lazy_const(
21582158
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
21592159
),
21602160
})

src/librustc/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
408408
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
409409
let substs = tcx.lift_to_global(&substs).unwrap();
410410
let evaluated = evaluated.subst(tcx, substs);
411-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
411+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
412412
}
413413
}
414414
} else {
@@ -420,7 +420,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
420420
promoted: None
421421
};
422422
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
423-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
423+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
424424
}
425425
}
426426
}

src/librustc/traits/query/normalize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
203203
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
204204
let substs = tcx.lift_to_global(&substs).unwrap();
205205
let evaluated = evaluated.subst(tcx, substs);
206-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
206+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
207207
}
208208
}
209209
} else {
@@ -215,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
215215
promoted: None,
216216
};
217217
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
218-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
218+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
219219
}
220220
}
221221
}

src/librustc/ty/codec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
252252
where D: TyDecoder<'a, 'tcx>,
253253
'tcx: 'a,
254254
{
255-
Ok(decoder.tcx().intern_lazy_const(Decodable::decode(decoder)?))
255+
Ok(decoder.tcx().mk_lazy_const(Decodable::decode(decoder)?))
256256
}
257257

258258
#[inline]

src/librustc/ty/context.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct CtxtInterners<'tcx> {
127127
goal: InternedSet<'tcx, GoalKind<'tcx>>,
128128
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
129129
projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>,
130+
lazy_const: InternedSet<'tcx, LazyConst<'tcx>>,
130131
}
131132

132133
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
@@ -144,6 +145,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
144145
goal: Default::default(),
145146
goal_list: Default::default(),
146147
projs: Default::default(),
148+
lazy_const: Default::default(),
147149
}
148150
}
149151

@@ -1072,10 +1074,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10721074
self.global_arenas.adt_def.alloc(def)
10731075
}
10741076

1075-
pub fn intern_const_alloc(
1076-
self,
1077-
alloc: Allocation,
1078-
) -> &'gcx Allocation {
1077+
pub fn intern_const_alloc(self, alloc: Allocation) -> &'gcx Allocation {
10791078
self.allocation_interner.borrow_mut().intern(alloc, |alloc| {
10801079
self.global_arenas.const_allocs.alloc(alloc)
10811080
})
@@ -1095,10 +1094,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10951094
})
10961095
}
10971096

1098-
pub fn intern_lazy_const(self, c: ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
1099-
self.global_interners.arena.alloc(c)
1100-
}
1101-
11021097
pub fn intern_layout(self, layout: LayoutDetails) -> &'gcx LayoutDetails {
11031098
self.layout_interner.borrow_mut().intern(layout, |layout| {
11041099
self.global_arenas.layout.alloc(layout)
@@ -2238,6 +2233,12 @@ impl<'tcx: 'lcx, 'lcx> Borrow<GoalKind<'lcx>> for Interned<'tcx, GoalKind<'tcx>>
22382233
}
22392234
}
22402235

2236+
impl<'tcx: 'lcx, 'lcx> Borrow<LazyConst<'lcx>> for Interned<'tcx, LazyConst<'tcx>> {
2237+
fn borrow<'a>(&'a self) -> &'a LazyConst<'lcx> {
2238+
&self.0
2239+
}
2240+
}
2241+
22412242
impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]>
22422243
for Interned<'tcx, List<ExistentialPredicate<'tcx>>> {
22432244
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
@@ -2344,7 +2345,8 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
23442345

23452346
direct_interners!('tcx,
23462347
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
2347-
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>
2348+
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>,
2349+
lazy_const: mk_lazy_const(|c: &LazyConst<'_>| keep_local(&c)) -> LazyConst<'tcx>
23482350
);
23492351

23502352
macro_rules! slice_interners {
@@ -2529,7 +2531,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25292531

25302532
#[inline]
25312533
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
2532-
self.mk_ty(Array(ty, self.intern_lazy_const(
2534+
self.mk_ty(Array(ty, self.mk_lazy_const(
25332535
ty::LazyConst::Evaluated(ty::Const::from_usize(self.global_tcx(), n))
25342536
)))
25352537
}

src/librustc/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
10421042
ty::LazyConst::Unevaluated(*def_id, substs.fold_with(folder))
10431043
}
10441044
};
1045-
folder.tcx().intern_lazy_const(new)
1045+
folder.tcx().mk_lazy_const(new)
10461046
}
10471047

10481048
fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {

src/librustc_mir/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
268268
span: expr_span,
269269
ty: this.hir.tcx().types.u32,
270270
user_ty: None,
271-
literal: this.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(
271+
literal: this.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
272272
ty::Const::from_bits(
273273
this.hir.tcx(),
274274
0,

src/librustc_mir/build/matches/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
302302
}
303303
let eq_def_id = self.hir.tcx().lang_items().eq_trait().unwrap();
304304
let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]);
305-
let method = self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(method));
305+
let method = self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(method));
306306

307307
let re_erased = self.hir.tcx().types.re_erased;
308308
// take the argument by reference

src/librustc_mir/build/misc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3333
span,
3434
ty,
3535
user_ty: None,
36-
literal: self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(literal)),
36+
literal: self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(literal)),
3737
};
3838
Operand::Constant(constant)
3939
}

src/librustc_mir/hair/cx/expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
342342
}
343343

344344
hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
345-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
345+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
346346
cx.const_eval_literal(&lit.node, expr_ty, lit.span, false)
347347
)),
348348
user_ty: None,
@@ -442,7 +442,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
442442
} else {
443443
if let hir::ExprKind::Lit(ref lit) = arg.node {
444444
ExprKind::Literal {
445-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
445+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
446446
cx.const_eval_literal(&lit.node, expr_ty, lit.span, true)
447447
)),
448448
user_ty: None,
@@ -702,7 +702,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
702702
ty: var_ty,
703703
span: expr.span,
704704
kind: ExprKind::Literal {
705-
literal: cx.tcx.intern_lazy_const(literal),
705+
literal: cx.tcx.mk_lazy_const(literal),
706706
user_ty: None
707707
},
708708
}.to_ref();
@@ -856,7 +856,7 @@ fn method_callee<'a, 'gcx, 'tcx>(
856856
ty,
857857
span,
858858
kind: ExprKind::Literal {
859-
literal: cx.tcx().intern_lazy_const(ty::LazyConst::Evaluated(
859+
literal: cx.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
860860
ty::Const::zero_sized(ty)
861861
)),
862862
user_ty,
@@ -918,7 +918,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
918918
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
919919
debug!("convert_path_expr: user_ty={:?}", user_ty);
920920
ExprKind::Literal {
921-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::zero_sized(
921+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::zero_sized(
922922
cx.tables().node_id_to_type(expr.hir_id),
923923
))),
924924
user_ty,
@@ -930,7 +930,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
930930
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
931931
debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
932932
ExprKind::Literal {
933-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Unevaluated(def_id, substs)),
933+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Unevaluated(def_id, substs)),
934934
user_ty,
935935
}
936936
},

src/librustc_mir/hair/cx/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
109109
}
110110

111111
pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::LazyConst<'tcx> {
112-
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
112+
self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
113113
}
114114

115115
pub fn bool_ty(&mut self) -> Ty<'tcx> {
@@ -121,11 +121,11 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
121121
}
122122

123123
pub fn true_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
124-
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
124+
self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
125125
}
126126

127127
pub fn false_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
128-
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
128+
self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
129129
}
130130

131131
pub fn const_eval_literal(

src/librustc_mir/hair/pattern/mod.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ pub enum PatternKind<'tcx> {
164164
},
165165
}
166166

167+
impl<'tcx> PatternKind<'tcx> {
168+
/// If this is a `PatternKind::AscribeUserType` then return the subpattern kind, otherwise
169+
/// return this pattern kind.
170+
fn with_user_type_ascription_subpattern(self) -> Self {
171+
match self {
172+
PatternKind::AscribeUserType { subpattern: Pattern { box kind, .. }, .. } => kind,
173+
kind => kind,
174+
}
175+
}
176+
}
177+
167178
#[derive(Clone, Copy, Debug, PartialEq)]
168179
pub struct PatternRange<'tcx> {
169180
pub lo: ty::Const<'tcx>,
@@ -400,9 +411,15 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
400411
PatKind::Lit(ref value) => self.lower_lit(value),
401412

402413
PatKind::Range(ref lo_expr, ref hi_expr, end) => {
403-
match (self.lower_lit(lo_expr), self.lower_lit(hi_expr)) {
404-
(PatternKind::Constant { value: lo },
405-
PatternKind::Constant { value: hi }) => {
414+
match (
415+
// Look for `PatternKind::Constant` patterns inside of any
416+
// `PatternKind::AscribeUserType` patterns. Type ascriptions can be safely
417+
// ignored for the purposes of lowering a range correctly - these are checked
418+
// elsewhere for well-formedness.
419+
self.lower_lit(lo_expr).with_user_type_ascription_subpattern(),
420+
self.lower_lit(hi_expr).with_user_type_ascription_subpattern(),
421+
) {
422+
(PatternKind::Constant { value: lo }, PatternKind::Constant { value: hi }) => {
406423
use std::cmp::Ordering;
407424
let cmp = compare_const_vals(
408425
self.tcx,
@@ -451,7 +468,15 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
451468
}
452469
}
453470
}
454-
_ => PatternKind::Wild
471+
ref pats => {
472+
self.tcx.sess.delay_span_bug(
473+
pat.span,
474+
&format!("found bad range pattern `{:?}` outside of error recovery",
475+
pats),
476+
);
477+
478+
PatternKind::Wild
479+
}
455480
}
456481
}
457482

src/librustc_mir/shim.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
459459
span: self.span,
460460
ty: func_ty,
461461
user_ty: None,
462-
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
462+
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
463463
ty::Const::zero_sized(func_ty),
464464
)),
465465
});
@@ -521,7 +521,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
521521
span: self.span,
522522
ty: self.tcx.types.usize,
523523
user_ty: None,
524-
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
524+
literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
525525
ty::Const::from_usize(self.tcx, value),
526526
)),
527527
}
@@ -759,7 +759,7 @@ fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
759759
span,
760760
ty,
761761
user_ty: None,
762-
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
762+
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
763763
ty::Const::zero_sized(ty)
764764
)),
765765
}),

src/librustc_mir/transform/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
533533
span,
534534
ty: self.tcx.types.bool,
535535
user_ty: None,
536-
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
536+
literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
537537
ty::Const::from_bool(self.tcx, val),
538538
)),
539539
})))

src/librustc_mir/transform/generator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
171171
span: source_info.span,
172172
ty: self.tcx.types.u32,
173173
user_ty: None,
174-
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
174+
literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
175175
self.tcx,
176176
state_disc.into(),
177177
ty::ParamEnv::empty().and(self.tcx.types.u32)
@@ -687,7 +687,7 @@ fn insert_panic_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
687687
span: mir.span,
688688
ty: tcx.types.bool,
689689
user_ty: None,
690-
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
690+
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
691691
ty::Const::from_bool(tcx, false),
692692
)),
693693
}),

src/librustc_mir/util/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
963963
span: self.source_info.span,
964964
ty: self.tcx().types.usize,
965965
user_ty: None,
966-
literal: self.tcx().intern_lazy_const(ty::LazyConst::Evaluated(
966+
literal: self.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
967967
ty::Const::from_usize(self.tcx(), val.into())
968968
)),
969969
})

0 commit comments

Comments
 (0)