Skip to content

Commit fee39ba

Browse files
committed
Auto merge of #47957 - bobtwinkles:fix_mir_consts, r=nikomatsakis
[NLL] Improve DefiningTy::Const Fixes #47590 by fixing the way DefiningTy represents constants. Previously, constants were represented using just the type of the variable. However, this will fail to capture early-bound regions as NLL inference vars, resulting in an ICE when we try to compute region VIDs a little bit later in the universal region resolution process. (ref #47590)
2 parents 4f93357 + e99f8fc commit fee39ba

File tree

4 files changed

+128
-38
lines changed

4 files changed

+128
-38
lines changed

src/librustc_mir/borrow_check/nll/region_infer/annotation.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ impl<'gcx, 'tcx> RegionInferenceContext<'tcx> {
4545
&substs[..]
4646
));
4747
}
48-
DefiningTy::Const(ty) => {
48+
DefiningTy::Const(def_id, substs) => {
4949
err.note(&format!(
50-
"defining type: {:?}",
51-
ty
50+
"defining constant type: {:?} with substs {:#?}",
51+
def_id,
52+
&substs[..]
5253
));
5354
}
5455
}

src/librustc_mir/borrow_check/nll/universal_regions.rs

+42-35
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub enum DefiningTy<'tcx> {
125125
/// The MIR represents some form of constant. The signature then
126126
/// is that it has no inputs and a single return value, which is
127127
/// the value of the constant.
128-
Const(Ty<'tcx>),
128+
Const(DefId, &'tcx Substs<'tcx>),
129129
}
130130

131131
#[derive(Debug)]
@@ -534,34 +534,42 @@ impl<'cx, 'gcx, 'tcx> UniversalRegionsBuilder<'cx, 'gcx, 'tcx> {
534534
/// see `DefiningTy` for details.
535535
fn defining_ty(&self) -> DefiningTy<'tcx> {
536536
let tcx = self.infcx.tcx;
537-
538537
let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id);
539538

540-
let defining_ty = if self.mir_def_id == closure_base_def_id {
541-
tcx.type_of(closure_base_def_id)
542-
} else {
543-
let tables = tcx.typeck_tables_of(self.mir_def_id);
544-
tables.node_id_to_type(self.mir_hir_id)
545-
};
546-
547-
let defining_ty = self.infcx
548-
.replace_free_regions_with_nll_infer_vars(FR, &defining_ty);
549-
550539
match tcx.hir.body_owner_kind(self.mir_node_id) {
551-
BodyOwnerKind::Fn => match defining_ty.sty {
552-
ty::TyClosure(def_id, substs) => DefiningTy::Closure(def_id, substs),
553-
ty::TyGenerator(def_id, substs, interior) => {
554-
DefiningTy::Generator(def_id, substs, interior)
540+
BodyOwnerKind::Fn => {
541+
let defining_ty = if self.mir_def_id == closure_base_def_id {
542+
tcx.type_of(closure_base_def_id)
543+
} else {
544+
let tables = tcx.typeck_tables_of(self.mir_def_id);
545+
tables.node_id_to_type(self.mir_hir_id)
546+
};
547+
548+
let defining_ty = self.infcx
549+
.replace_free_regions_with_nll_infer_vars(FR, &defining_ty);
550+
551+
match defining_ty.sty {
552+
ty::TyClosure(def_id, substs) => DefiningTy::Closure(def_id, substs),
553+
ty::TyGenerator(def_id, substs, interior) => {
554+
DefiningTy::Generator(def_id, substs, interior)
555+
}
556+
ty::TyFnDef(def_id, substs) => DefiningTy::FnDef(def_id, substs),
557+
_ => span_bug!(
558+
tcx.def_span(self.mir_def_id),
559+
"expected defining type for `{:?}`: `{:?}`",
560+
self.mir_def_id,
561+
defining_ty
562+
),
555563
}
556-
ty::TyFnDef(def_id, substs) => DefiningTy::FnDef(def_id, substs),
557-
_ => span_bug!(
558-
tcx.def_span(self.mir_def_id),
559-
"expected defining type for `{:?}`: `{:?}`",
560-
self.mir_def_id,
561-
defining_ty
562-
),
563-
},
564-
BodyOwnerKind::Const | BodyOwnerKind::Static(..) => DefiningTy::Const(defining_ty),
564+
}
565+
566+
BodyOwnerKind::Const | BodyOwnerKind::Static(..) => {
567+
assert_eq!(closure_base_def_id, self.mir_def_id);
568+
let identity_substs = Substs::identity_for_item(tcx, closure_base_def_id);
569+
let substs = self.infcx
570+
.replace_free_regions_with_nll_infer_vars(FR, &identity_substs);
571+
DefiningTy::Const(self.mir_def_id, substs)
572+
}
565573
}
566574
}
567575

@@ -592,13 +600,7 @@ impl<'cx, 'gcx, 'tcx> UniversalRegionsBuilder<'cx, 'gcx, 'tcx> {
592600
substs.substs
593601
}
594602

595-
DefiningTy::FnDef(_, substs) => substs,
596-
597-
// When we encounter a constant body, just return whatever
598-
// substitutions are in scope for that constant.
599-
DefiningTy::Const(_) => {
600-
identity_substs
601-
}
603+
DefiningTy::FnDef(_, substs) | DefiningTy::Const(_, substs) => substs,
602604
};
603605

604606
let global_mapping = iter::once((gcx.types.re_static, fr_static));
@@ -660,9 +662,14 @@ impl<'cx, 'gcx, 'tcx> UniversalRegionsBuilder<'cx, 'gcx, 'tcx> {
660662
sig.inputs_and_output()
661663
}
662664

663-
// For a constant body, there are no inputs, and one
664-
// "output" (the type of the constant).
665-
DefiningTy::Const(ty) => ty::Binder::dummy(tcx.mk_type_list(iter::once(ty))),
665+
DefiningTy::Const(def_id, _) => {
666+
// For a constant body, there are no inputs, and one
667+
// "output" (the type of the constant).
668+
assert_eq!(self.mir_def_id, def_id);
669+
let ty = tcx.type_of(def_id);
670+
let ty = indices.fold_to_region_vids(tcx, &ty);
671+
ty::Binder::dummy(tcx.mk_type_list(iter::once(ty)))
672+
}
666673
}
667674
}
668675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test cases where we put various lifetime constraints on trait
12+
// associated constants.
13+
14+
#![feature(rustc_attrs)]
15+
16+
use std::option::Option;
17+
18+
trait Anything<'a: 'b, 'b> {
19+
const AC: Option<&'b str>;
20+
}
21+
22+
struct OKStruct { }
23+
24+
impl<'a: 'b, 'b> Anything<'a, 'b> for OKStruct {
25+
const AC: Option<&'b str> = None;
26+
}
27+
28+
struct FailStruct1 { }
29+
30+
impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
31+
const AC: Option<&'c str> = None;
32+
//~^ ERROR: mismatched types
33+
}
34+
35+
struct FailStruct2 { }
36+
37+
impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
38+
const AC: Option<&'a str> = None;
39+
//~^ ERROR: mismatched types
40+
}
41+
42+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/trait-associated-constant.rs:31:5
3+
|
4+
31 | const AC: Option<&'c str> = None;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
6+
|
7+
= note: expected type `std::option::Option<&'b str>`
8+
found type `std::option::Option<&'c str>`
9+
note: the lifetime 'c as defined on the impl at 30:1...
10+
--> $DIR/trait-associated-constant.rs:30:1
11+
|
12+
30 | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
note: ...does not necessarily outlive the lifetime 'b as defined on the impl at 30:1
15+
--> $DIR/trait-associated-constant.rs:30:1
16+
|
17+
30 | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/trait-associated-constant.rs:38:5
22+
|
23+
38 | const AC: Option<&'a str> = None;
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
25+
|
26+
= note: expected type `std::option::Option<&'b str>`
27+
found type `std::option::Option<&'a str>`
28+
note: the lifetime 'a as defined on the impl at 37:1...
29+
--> $DIR/trait-associated-constant.rs:37:1
30+
|
31+
37 | impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
note: ...does not necessarily outlive the lifetime 'b as defined on the impl at 37:1
34+
--> $DIR/trait-associated-constant.rs:37:1
35+
|
36+
37 | impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error: aborting due to 2 previous errors
40+

0 commit comments

Comments
 (0)