Skip to content

Commit c471519

Browse files
committed
Auto merge of #59369 - oli-obk:unwrap_usICE, r=eddyb,nikomatsakis
`unwrap_usize` should at least try to evaluate the underlying constant r? @eddyb fixes #59016 I know that I'm still using `ParamEnv` wrongly, but that's a preexisting issue not amplified by this PR.
2 parents f6ecdc2 + bd57498 commit c471519

File tree

53 files changed

+298
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+298
-142
lines changed

src/librustc/infer/combine.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
340340
ambient_variance,
341341
needs_wf: false,
342342
root_ty: ty,
343+
param_env: self.param_env,
343344
};
344345

345346
let ty = match generalize.relate(&ty, &ty) {
@@ -379,6 +380,8 @@ struct Generalizer<'cx, 'tcx> {
379380

380381
/// The root type that we are generalizing. Used when reporting cycles.
381382
root_ty: Ty<'tcx>,
383+
384+
param_env: ty::ParamEnv<'tcx>,
382385
}
383386

384387
/// Result from a generalization operation. This includes
@@ -419,6 +422,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
419422
fn tcx(&self) -> TyCtxt<'tcx> {
420423
self.infcx.tcx
421424
}
425+
fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env }
422426

423427
fn tag(&self) -> &'static str {
424428
"Generalizer"

src/librustc/infer/equate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> {
3030

3131
fn tcx(&self) -> TyCtxt<'tcx> { self.fields.tcx() }
3232

33+
fn param_env(&self) -> ty::ParamEnv<'tcx> { self.fields.param_env }
34+
3335
fn a_is_expected(&self) -> bool { self.a_is_expected }
3436

3537
fn relate_item_substs(&mut self,

src/librustc/infer/glb.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ impl TypeRelation<'tcx> for Glb<'combine, 'infcx, 'tcx> {
2727

2828
fn tcx(&self) -> TyCtxt<'tcx> { self.fields.tcx() }
2929

30+
fn param_env(&self) -> ty::ParamEnv<'tcx> { self.fields.param_env }
31+
3032
fn a_is_expected(&self) -> bool { self.a_is_expected }
3133

3234
fn relate_with_variance<T: Relate<'tcx>>(&mut self,

src/librustc/infer/lub.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> {
2727

2828
fn tcx(&self) -> TyCtxt<'tcx> { self.fields.tcx() }
2929

30+
fn param_env(&self) -> ty::ParamEnv<'tcx> { self.fields.param_env }
31+
3032
fn a_is_expected(&self) -> bool { self.a_is_expected }
3133

3234
fn relate_with_variance<T: Relate<'tcx>>(&mut self,

src/librustc/infer/nll_relate/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ where
502502
self.infcx.tcx
503503
}
504504

505+
// FIXME(oli-obk): not sure how to get the correct ParamEnv
506+
fn param_env(&self) -> ty::ParamEnv<'tcx> { ty::ParamEnv::empty() }
507+
505508
fn tag(&self) -> &'static str {
506509
"nll::subtype"
507510
}
@@ -831,6 +834,9 @@ where
831834
self.infcx.tcx
832835
}
833836

837+
// FIXME(oli-obk): not sure how to get the correct ParamEnv
838+
fn param_env(&self) -> ty::ParamEnv<'tcx> { ty::ParamEnv::empty() }
839+
834840
fn tag(&self) -> &'static str {
835841
"nll::generalizer"
836842
}

src/librustc/infer/outlives/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::ty::{self, Ty};
2727
/// interested in the `OutlivesEnvironment`. -nmatsakis
2828
#[derive(Clone)]
2929
pub struct OutlivesEnvironment<'tcx> {
30-
param_env: ty::ParamEnv<'tcx>,
30+
pub param_env: ty::ParamEnv<'tcx>,
3131
free_region_map: FreeRegionMap<'tcx>,
3232

3333
// Contains, for each body B that we are checking (that is, the fn

src/librustc/infer/sub.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ impl<'combine, 'infcx, 'tcx> Sub<'combine, 'infcx, 'tcx> {
3535
impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
3636
fn tag(&self) -> &'static str { "Sub" }
3737
fn tcx(&self) -> TyCtxt<'tcx> { self.fields.infcx.tcx }
38+
39+
fn param_env(&self) -> ty::ParamEnv<'tcx> { self.fields.param_env }
40+
3841
fn a_is_expected(&self) -> bool { self.a_is_expected }
3942

4043
fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
277277
) -> Self {
278278
ExprUseVisitor {
279279
mc: mc::MemCategorizationContext::new(tcx,
280+
param_env,
280281
body_owner,
281282
region_scope_tree,
282283
tables,
@@ -299,6 +300,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
299300
ExprUseVisitor {
300301
mc: mc::MemCategorizationContext::with_infer(
301302
infcx,
303+
param_env,
302304
body_owner,
303305
region_scope_tree,
304306
tables,

src/librustc/middle/mem_categorization.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl HirNode for hir::Pat {
214214
#[derive(Clone)]
215215
pub struct MemCategorizationContext<'a, 'tcx> {
216216
pub tcx: TyCtxt<'tcx>,
217+
param_env: ty::ParamEnv<'tcx>,
217218
pub body_owner: DefId,
218219
pub upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
219220
pub region_scope_tree: &'a region::ScopeTree,
@@ -330,6 +331,7 @@ impl MutabilityCategory {
330331
impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
331332
pub fn new(
332333
tcx: TyCtxt<'tcx>,
334+
param_env: ty::ParamEnv<'tcx>,
333335
body_owner: DefId,
334336
region_scope_tree: &'a region::ScopeTree,
335337
tables: &'a ty::TypeckTables<'tcx>,
@@ -342,7 +344,8 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
342344
region_scope_tree,
343345
tables,
344346
rvalue_promotable_map,
345-
infcx: None
347+
infcx: None,
348+
param_env,
346349
}
347350
}
348351
}
@@ -359,6 +362,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
359362
/// known, the results around upvar accesses may be incorrect.
360363
pub fn with_infer(
361364
infcx: &'a InferCtxt<'a, 'tcx>,
365+
param_env: ty::ParamEnv<'tcx>,
362366
body_owner: DefId,
363367
region_scope_tree: &'a region::ScopeTree,
364368
tables: &'a ty::TypeckTables<'tcx>,
@@ -379,6 +383,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
379383
tables,
380384
rvalue_promotable_map,
381385
infcx: Some(infcx),
386+
param_env,
382387
}
383388
}
384389

@@ -896,7 +901,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
896901

897902
// Always promote `[T; 0]` (even when e.g., borrowed mutably).
898903
let promotable = match expr_ty.sty {
899-
ty::Array(_, len) if len.assert_usize(self.tcx) == Some(0) => true,
904+
ty::Array(_, len) if len.try_eval_usize(self.tcx, self.param_env) == Some(0) => true,
900905
_ => promotable,
901906
};
902907

src/librustc/mir/tcx.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'tcx> PlaceTy<'tcx> {
5757
/// `PlaceElem`, where we can just use the `Ty` that is already
5858
/// stored inline on field projection elems.
5959
pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: &PlaceElem<'tcx>) -> PlaceTy<'tcx> {
60-
self.projection_ty_core(tcx, elem, |_, _, ty| ty)
60+
self.projection_ty_core(tcx, ty::ParamEnv::empty(), elem, |_, _, ty| ty)
6161
}
6262

6363
/// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
@@ -68,6 +68,7 @@ impl<'tcx> PlaceTy<'tcx> {
6868
pub fn projection_ty_core<V, T>(
6969
self,
7070
tcx: TyCtxt<'tcx>,
71+
param_env: ty::ParamEnv<'tcx>,
7172
elem: &ProjectionElem<V, T>,
7273
mut handle_field: impl FnMut(&Self, &Field, &T) -> Ty<'tcx>,
7374
) -> PlaceTy<'tcx>
@@ -90,7 +91,7 @@ impl<'tcx> PlaceTy<'tcx> {
9091
ProjectionElem::Subslice { from, to } => {
9192
PlaceTy::from_ty(match self.ty.sty {
9293
ty::Array(inner, size) => {
93-
let size = size.unwrap_usize(tcx);
94+
let size = size.eval_usize(tcx, param_env);
9495
let len = size - (from as u64) - (to as u64);
9596
tcx.mk_array(inner, len)
9697
}

0 commit comments

Comments
 (0)