Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NLL] issue-34780.rs test ICEs #47590

Closed
spastorino opened this issue Jan 19, 2018 · 8 comments · Fixed by #47957
Closed

[NLL] issue-34780.rs test ICEs #47590

spastorino opened this issue Jan 19, 2018 · 8 comments · Fixed by #47957
Assignees
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@spastorino
Copy link
Member

$ RUST_BACKTRACE=full rustc +stage1 src/test/run-pass/issue-34780.rs -Znll -Zborrowck=mir -Ztwo-phase-borrows
error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.25.0-dev running on x86_64-unknown-linux-gnu

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'assertion failed: identity_substs.is_empty()', librustc_mir/borrow_check/nll/universal_regions.rs:593:17
stack backtrace:
   0:     0x7f60f72890fb - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::h89e4e024185eb251
                               at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1:     0x7f60f729d861 - std::sys_common::backtrace::print::h2c98df9d22a6e204
                               at libstd/sys_common/backtrace.rs:68
                               at libstd/sys_common/backtrace.rs:57
   2:     0x7f60f727e651 - std::panicking::default_hook::{{closure}}::hc4353cdd7897b77b
                               at libstd/panicking.rs:380
   3:     0x7f60f727e271 - std::panicking::default_hook::h9bbd1477c233f26c
                               at libstd/panicking.rs:390
   4:     0x7f60f727e946 - std::panicking::rust_panic_with_hook::hdfded1d66d2e81cf
                               at libstd/panicking.rs:576
   5:     0x7f60f4f6e381 - std::panicking::begin_panic::hd19c2591672191ce
                               at /home/santiago/src/oss/rust1/src/libstd/panicking.rs:537
   6:     0x7f60f4dc116e - rustc_mir::borrow_check::nll::universal_regions::UniversalRegions::new::hf70879742af60a25
                               at librustc_mir/borrow_check/nll/universal_regions.rs:593
                               at librustc_mir/borrow_check/nll/universal_regions.rs:448
                               at librustc_mir/borrow_check/nll/universal_regions.rs:215
   7:     0x7f60f4d99b1b - rustc_mir::borrow_check::nll::replace_regions_in_mir::haf8b054c38d5b9c9
                               at librustc_mir/borrow_check/nll/mod.rs:54
   8:     0x7f60f4d9173e - rustc_mir::borrow_check::do_mir_borrowck::h5527e035bdb6f90b
                               at librustc_mir/borrow_check/mod.rs:110
   9:     0x7f60f4f6ebcc - <std::thread::local::LocalKey<T>>::with::h09e3d281217138a6
                               at librustc_mir/borrow_check/mod.rs:81
                               at /home/santiago/src/oss/rust1/src/librustc/infer/mod.rs:439
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1573
                               at /home/santiago/src/oss/rust1/src/libstd/thread/local.rs:377
                               at /home/santiago/src/oss/rust1/src/libstd/thread/local.rs:288
  10:     0x7f60f4d62f0a - rustc::ty::context::tls::enter::hf92705338ff04233
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1570
  11:     0x7f60f4d6566c - rustc::ty::context::GlobalCtxt::enter_local::h834852a1d0d3a09a
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1381
  12:     0x7f60f4d4e3f2 - rustc::infer::InferCtxtBuilder::enter::h876297f802958ad2
                               at /home/santiago/src/oss/rust1/src/librustc/infer/mod.rs:439
  13:     0x7f60f4d914ad - rustc_mir::borrow_check::mir_borrowck::ha3ddcadee42ed870
                               at librustc_mir/borrow_check/mod.rs:79
  14:     0x7f60f2cf1b90 - rustc::ty::maps::<impl rustc::ty::maps::queries::mir_borrowck<'tcx>>::compute_result::hf45a226b5c044941
                               at librustc/ty/maps/plumbing.rs:383
  15:     0x7f60f293c108 - rustc::dep_graph::graph::DepGraph::with_task_impl::h977b285d649164fa
                               at librustc/dep_graph/graph.rs:289
  16:     0x7f60f290ea0f - rustc::dep_graph::graph::DepGraph::with_task::h606c300c86adccb4
                               at librustc/dep_graph/graph.rs:199
  17:     0x7f60f2e3b057 - rustc_errors::Handler::track_diagnostics::h5c4733a59e59914c
                               at librustc/ty/maps/plumbing.rs:492
                               at /home/santiago/src/oss/rust1/src/librustc_errors/lib.rs:572
  18:     0x7f60f2bbeace - rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::cycle_check::h510dda0193aa77a2
                               at librustc/ty/maps/plumbing.rs:485
                               at librustc/ty/maps/plumbing.rs:121
  19:     0x7f60f2cf1c92 - rustc::ty::maps::<impl rustc::ty::maps::queries::mir_borrowck<'tcx>>::force::h5b5a43ddd69d4127
                               at librustc/ty/maps/plumbing.rs:484
  20:     0x7f60f2cf2921 - rustc::ty::maps::<impl rustc::ty::maps::queries::mir_borrowck<'tcx>>::try_get::hb7409412e9075295
                               at librustc/ty/maps/plumbing.rs:301
                               at librustc/ty/maps/plumbing.rs:526
  21:     0x7f60f2c46015 - rustc::ty::maps::TyCtxtAt::mir_borrowck::hb6ba692bbeabd5e9
                               at librustc/ty/maps/plumbing.rs:565
  22:     0x7f60f2b89718 - rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_borrowck::h44d6f533e09947b5
                               at librustc/ty/maps/plumbing.rs:558
  23:     0x7f60f7611155 - rustc_driver::driver::phase_3_run_analysis_passes::{{closure}}::{{closure}}::h5b4653d0f1e2de53
                               at librustc_driver/driver.rs:1073
  24:     0x7f60f76d8226 - <std::thread::local::LocalKey<T>>::with::h5c65293ff59250fe
                               at librustc_driver/driver.rs:1071
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1573
                               at /home/santiago/src/oss/rust1/src/libstd/thread/local.rs:377
                               at /home/santiago/src/oss/rust1/src/libstd/thread/local.rs:288
  25:     0x7f60f75f571e - rustc::ty::context::tls::enter::h88b739b20317ffe4
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1570
  26:     0x7f60f76d6cec - <std::thread::local::LocalKey<T>>::with::h43c40e0d4e18765b
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1557
                               at /home/santiago/src/oss/rust1/src/libstd/thread/local.rs:377
                               at /home/santiago/src/oss/rust1/src/libstd/thread/local.rs:288
  27:     0x7f60f75f51d7 - rustc::ty::context::tls::enter_global::h0e8e2e22f658e325
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1554
  28:     0x7f60f75f9bf5 - rustc::ty::context::TyCtxt::create_and_enter::h1843d08844146f02
                               at /home/santiago/src/oss/rust1/src/librustc/ty/context.rs:1197
  29:     0x7f60f763ee27 - rustc_driver::driver::phase_3_run_analysis_passes::hdca06791b92b8d71
                               at librustc_driver/driver.rs:1017
  30:     0x7f60f7630c57 - rustc_driver::driver::compile_input::h80b1c15489d523ea
                               at librustc_driver/driver.rs:220
  31:     0x7f60f7622646 - rustc_driver::run_compiler::h6941f27d9cf14ec2
                               at librustc_driver/lib.rs:248
  32:     0x7f60f76cf97a - std::sys_common::backtrace::__rust_begin_short_backtrace::h2f40e38beb20488f
                               at librustc_driver/lib.rs:1305
                               at librustc_driver/lib.rs:132
                               at librustc_driver/lib.rs:1212
                               at /home/santiago/src/oss/rust1/src/libstd/sys_common/backtrace.rs:133
  33:     0x7f60f72b2e76 - __rust_maybe_catch_panic
                               at libpanic_unwind/lib.rs:102
  34:     0x7f60f76b2437 - std::panicking::try::hf10cda8586478214
                               at /home/santiago/src/oss/rust1/src/libstd/panicking.rs:458
  35:     0x7f60f76b227d - std::panic::catch_unwind::h95b8c396b2db40d5
                               at /home/santiago/src/oss/rust1/src/libstd/panic.rs:358
  36:     0x7f60f76e49f3 - <F as alloc::boxed::FnBox<A>>::call_box::h6aa43ee35a088e4c
                               at /home/santiago/src/oss/rust1/src/libstd/thread/mod.rs:405
                               at /home/santiago/src/oss/rust1/src/liballoc/boxed.rs:817
  37:     0x7f60f72a5792 - std::sys_common::thread::start_thread::he35a8e58b745ed7d
                               at libstd/sys_common/thread.rs:24
  38:     0x7f60f729a408 - std::sys::unix::thread::Thread::new::thread_start::h669389680624d627
                               at libstd/sys/unix/thread.rs:90
  39:     0x7f60f0af508b - start_thread
  40:     0x7f60f6f48e1e - __GI___clone
  41:                0x0 - <unknown>
@spastorino
Copy link
Member Author

#47577 is probably the same issue.

@nikomatsakis nikomatsakis added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-NLL Area: Non-lexical lifetimes (NLL) WG-compiler-nll I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Jan 19, 2018
@nikomatsakis nikomatsakis added this to the NLL: Valid code works milestone Jan 19, 2018
@nikomatsakis
Copy link
Contributor

Not a dup of #47153

@nikomatsakis
Copy link
Contributor

or rather, with the fix applied, I get another ICE =)

@nikomatsakis
Copy link
Contributor

error: internal compiler error: librustc_mir/borrow_check/nll/mod.rs:293: region is not an ReVar: ReEarlyBound(1, 'a)

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.25.0-dev running on x86_64-unknown-linux-gnu

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Box<Any>', librustc_errors/lib.rs:509:9
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:68
             at libstd/sys_common/backtrace.rs:57
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:380
   3: std::panicking::default_hook
             at libstd/panicking.rs:390
   4: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:576
   5: std::panicking::begin_panic
             at /home/nmatsakis/versioned/rust-9/src/libstd/panicking.rs:537
   6: rustc_errors::Handler::bug
             at librustc_errors/lib.rs:509
   7: <std::thread::local::LocalKey<T>>::with
             at librustc/session/mod.rs:1137
             at librustc/ty/context.rs:1600
             at librustc/ty/context.rs:1589
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:377
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:288
   8: rustc::ty::context::tls::with_opt
             at librustc/ty/context.rs:1585
             at librustc/ty/context.rs:1600
   9: rustc::session::opt_span_bug_fmt
             at librustc/session/mod.rs:1133
  10: rustc::session::bug_fmt
             at librustc/session/mod.rs:1117
  11: rustc_mir::borrow_check::nll::universal_regions::UniversalRegions::new
             at librustc_mir/borrow_check/nll/mod.rs:293
             at librustc_mir/borrow_check/nll/universal_regions.rs:597
             at /home/nmatsakis/versioned/rust-9/src/libcore/ops/function.rs:271
             at /home/nmatsakis/versioned/rust-9/src/libcore/option.rs:404
             at /home/nmatsakis/versioned/rust-9/src/libcore/iter/mod.rs:1251
             at /home/nmatsakis/versioned/rust-9/src/libcore/iter/mod.rs:1031
             at /home/nmatsakis/versioned/rust-9/src/libcore/option.rs:616
             at /home/nmatsakis/versioned/rust-9/src/libcore/iter/mod.rs:1030
             at /home/nmatsakis/versioned/rust-9/src/libcore/iter/mod.rs:981
             at /home/nmatsakis/versioned/rust-9/src/libcore/iter/mod.rs:758
             at /home/nmatsakis/versioned/rust-9/src/libstd/collections/hash/map.rs:2466
             at /home/nmatsakis/versioned/rust-9/src/libstd/collections/hash/map.rs:2444
             at /home/nmatsakis/versioned/rust-9/src/libcore/iter/iterator.rs:1298
             at librustc_mir/borrow_check/nll/universal_regions.rs:600
             at librustc_mir/borrow_check/nll/universal_regions.rs:448
             at librustc_mir/borrow_check/nll/universal_regions.rs:215
  12: rustc_mir::borrow_check::nll::replace_regions_in_mir
             at librustc_mir/borrow_check/nll/mod.rs:54
  13: rustc_mir::borrow_check::do_mir_borrowck
             at librustc_mir/borrow_check/mod.rs:110
  14: rustc::ty::context::tls::enter
             at librustc_mir/borrow_check/mod.rs:81
             at /home/nmatsakis/versioned/rust-9/src/librustc/infer/mod.rs:439
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1573
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:377
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:288
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1570
  15: rustc::infer::InferCtxtBuilder::enter
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1381
             at /home/nmatsakis/versioned/rust-9/src/librustc/infer/mod.rs:439
  16: rustc_mir::borrow_check::mir_borrowck
             at librustc_mir/borrow_check/mod.rs:79
  17: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_borrowck<'tcx>>::compute_result
             at librustc/ty/maps/plumbing.rs:383
  18: rustc::dep_graph::graph::DepGraph::with_task_impl
             at librustc/dep_graph/graph.rs:289
  19: rustc_errors::Handler::track_diagnostics
             at librustc/dep_graph/graph.rs:199
             at librustc/ty/maps/plumbing.rs:492
             at /home/nmatsakis/versioned/rust-9/src/librustc_errors/lib.rs:572
  20: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::cycle_check
             at librustc/ty/maps/plumbing.rs:485
             at librustc/ty/maps/plumbing.rs:121
  21: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_borrowck<'tcx>>::force
             at librustc/ty/maps/plumbing.rs:484
  22: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_borrowck<'tcx>>::try_get
             at librustc/ty/maps/plumbing.rs:301
             at librustc/ty/maps/plumbing.rs:526
  23: rustc::ty::maps::TyCtxtAt::mir_borrowck
             at librustc/ty/maps/plumbing.rs:565
  24: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_borrowck
             at librustc/ty/maps/plumbing.rs:558
  25: rustc_driver::driver::phase_3_run_analysis_passes::{{closure}}::{{closure}}
             at librustc_driver/driver.rs:1073
  26: <std::thread::local::LocalKey<T>>::with
             at /home/nmatsakis/versioned/rust-9/src/librustc/util/common.rs:120
             at librustc_driver/driver.rs:1071
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1573
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:377
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:288
  27: <std::thread::local::LocalKey<T>>::with
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1570
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1557
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:377
             at /home/nmatsakis/versioned/rust-9/src/libstd/thread/local.rs:288
  28: rustc::ty::context::TyCtxt::create_and_enter
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1554
             at /home/nmatsakis/versioned/rust-9/src/librustc/ty/context.rs:1197
  29: rustc_driver::driver::compile_input
             at librustc_driver/driver.rs:1017
             at librustc_driver/driver.rs:220
  30: rustc_driver::run_compiler
             at librustc_driver/lib.rs:248

@spastorino
Copy link
Member Author

That new ICE seems similar to #47587 :)

@nikomatsakis
Copy link
Contributor

Hmm, yes, but it's definitely not an issue with yield_ty

@cuviper cuviper added the C-bug Category: This is a bug. label Jan 27, 2018
@sapphire-arches sapphire-arches self-assigned this Jan 29, 2018
@sapphire-arches
Copy link
Contributor

This probably has something to do with the fact that consts use the identity substitutions when computing universal regions substitutions:

let fr_substs = match defining_ty {
DefiningTy::Closure(_, substs) | DefiningTy::Generator(_, substs, _) => {
// In the case of closures, we rely on the fact that
// the first N elements in the ClosureSubsts are
// inherited from the `closure_base_def_id`.
// Therefore, when we zip together (below) with
// `identity_substs`, we will get only those regions
// that correspond to early-bound regions declared on
// the `closure_base_def_id`.
assert!(substs.substs.len() >= identity_substs.len());
assert_eq!(substs.substs.regions().count(), identity_substs.regions().count());
substs.substs
}
DefiningTy::FnDef(_, substs) => substs,
// When we encounter a constant body, just return whatever
// substitutions are in scope for that constant.
DefiningTy::Const(_) => {
identity_substs
}
};
let global_mapping = iter::once((gcx.types.re_static, fr_static));
let subst_mapping = identity_substs
.regions()
.zip(fr_substs.regions().map(|r| r.to_region_vid()));

since those are always going to be generating ReEarlyBound regions:
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
-> &'tcx Substs<'tcx> {
Substs::for_item(tcx, def_id, |def, _| {
tcx.mk_region(ty::ReEarlyBound(def.to_early_bound_region_data()))
}, |def, _| tcx.mk_param_from_def(def))
}

I have yet to track down the code that should be resolving those early bounds and why it isn't doing so.

@nikomatsakis
Copy link
Contributor

Hmm, @bobtwinkles you are in the right area of the code. But I think the problem runs just a touch deeper. Actually the comments here are not that great. I should do a pass to improve them. But let me try to first explain what's happening a bit and then how to fix it.

What is the "defining type"

The role of the DefiningTy enum (which should perhaps be renamed?) is to kind of capture the "external signature" for a given piece of MIR, and ideally in a way that identifies all of the "degrees of freedom" that the code has (and just those degrees of freedom). Based just on the defining-type, we should be able to identify the following information:

  • The universally bound regions (i.e., lifetime parameters), both early and (where relevant) late bound
  • The types of all the MIR inputs (expressed in terms of these universally bound regions)
  • The types of the MIR output (expressed in terms of these universally bound regions)

This is perhaps easiest to explain with a function item. Assume that we are working with the MIR for this function foo:

fn foo<'a, T, U>(x: &'a T, y: &'a U) where T: Trait<'a> { ... }

In that case, the defining type will be the FnDef variant:

/// The MIR is a fn item with the given def-id and substs. The signature
/// of the function can be bound then with the `fn_sig` query.
FnDef(DefId, &'tcx Substs<'tcx>),

You can see that it is actually sort of minimal: just a def-id and a substs array. In this case, the substs array will consist of three things, first a lifetime variable (representing 'a) and then two types (TyParam, as it happens):

['?X, T, U]

(This substs array gets created in defining_ty on this line of code; this will basically replace the formal lifetime parameter of 'a (represented as an ReEarlyBound) with a fresh variable ?X.)

Given the substs array, we can easily compute the set of region variables representing universally bound regions in a function signature -- it's just the region variables appearing in the substs array. That's what we use to build the indices map in compute_indices.

Note that this substs array does not, for example, include the types of the arguments etc. That's because those types can be reconstructed using queries. For example, the compute_inputs_and_output function includes this code, which extracts the function signature (using the fn_sig query) and then basically substitutes (the indices.fold_to_region_vids(...) call has that effect) from the formal lifetime parameters:

DefiningTy::FnDef(def_id, _) => {
let sig = tcx.fn_sig(def_id);
let sig = indices.fold_to_region_vids(tcx, &sig);
sig.inputs_and_output()

Part of the reason it is done this way is that then the types of both arguments (&'a T and &'a U) will reference the same lifetime ('X). If, when creating the defining type, we had invoked replace_free_regions_with_nll_infer_vars on something that contained the types of those arguments separately, then they would have been rewritten to use two different lifetime variables. This would have been too many "degrees of freedom".

How does this relate to constants?

For constants, our defining-type variant is currently the type of the constant:

/// The MIR represents some form of constant. The signature then
/// is that it has no inputs and a single return value, which is
/// the value of the constant.
Const(Ty<'tcx>),

This is actually wrong, and is roughly the equivalent of storing the types of the arguments directly in the DefiningTy. The fault arises from this code in defining_ty -- it was written with functions and closures in mind. It fetches the "type" associated with the def-id and rewrites all the regions in it. This is ok for functions and closures because the type of a function or closure is already a kind of indirection; e.g., a TyFnDef is basically just the def-id+substs pair we want.

We probably want to change that variant to something more like Fn:

 Const(DefId, Substs<'tcx>),

then we want to do is to refactor defining_ty somewhat. I would expect it to look more like the following. This is the same as the existing code, just rearranged so that it only executes for BodyOwnedKind::Fn:

match tcx.hir.body_owner_kind(self.mir_node_id) {
    BodyOwnerKind::Fn => {
        let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id);

        let defining_ty = if self.mir_def_id == closure_base_def_id {
            tcx.type_of(closure_base_def_id)
        } else {
            let tables = tcx.typeck_tables_of(self.mir_def_id);
            tables.node_id_to_type(self.mir_hir_id)
        };

        let defining_ty = self.infcx.replace_free_regions_with_nll_infer_vars(FR, &defining_ty);

        match defining_ty.sty {
                ty::TyClosure(def_id, substs) => DefiningTy::Closure(def_id, substs),
                ty::TyGenerator(def_id, substs, interior) => {
                    DefiningTy::Generator(def_id, substs, interior)
                }
                ty::TyFnDef(def_id, substs) => DefiningTy::FnDef(def_id, substs),
                _ => span_bug!(
                    tcx.def_span(self.mir_def_id),
                    "expected defining type for `{:?}`: `{:?}`",
                    self.mir_def_id,
                    defining_ty
                ),
        }
    }

    BodyOwnerKind::Const | BodyOwnerKind::Static(..) => {
        // new code will go here, see below
    }
}

meanwhile, for Const and Static, we'll do something slightly different. We don't want to start by fetching tcx.type_of(def_id), because that gets the type associated with the static. Instead, we'll do:

assert_eq!(tcx.closure_base_def_id(self.mir_def_id), self.mir_def_id);
let identity_substs = Substs::identity_for_item(gcx, closure_base_def_id);
let substs =  self.infcx.replace_free_regions_with_nll_infer_vars(FR, &identity_substs);
DefiningTy::Const(self.mir_def_id, substs)

Then we can change the other bits of code to match. For example:

DefiningTy::Const(_) => {
identity_substs
}

becomes

DefiningTy::Const(_, substs) => substs,

When we wish to compute the "return type" of the closure, then we can do the type_of query:

// For a constant body, there are no inputs, and one
// "output" (the type of the constant).
DefiningTy::Const(ty) => ty::Binder::dummy(tcx.mk_type_list(iter::once(ty))),

becomes:

// For a constant body, there are no inputs, and one
// "output" (the type of the constant).
DefiningTy::Const(def_id, substs) => {
    let ty = tcx.type_of(def_id);
    let ty = indices.fold_to_region_vids(tcx, &ty); // equivalently, `ty.subst(substs)` would work
    ty::Binder::dummy(tcx.mk_type_list(iter::once(ty)))
}

I think that ought to work.

cc @bobtwinkles

sapphire-arches added a commit to sapphire-arches/rust that referenced this issue Feb 7, 2018
Fixes rust-lang#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.
bors added a commit that referenced this issue Feb 7, 2018
[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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants