Skip to content

Commit c96eb70

Browse files
committed
Fix and test upvar name printing for validity
1 parent 69576fc commit c96eb70

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,17 +559,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
559559
match layout.ty.sty {
560560
// generators and closures.
561561
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
562-
if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
563-
if let Some(freevar) = self.tcx.with_freevars(
564-
node_id,
565-
|fv| fv.get(field).map(|field| *field))
566-
{
567-
return PathElem::ClosureVar(self.tcx.hir.name(freevar.var_id()));
568-
}
562+
if let Some(upvar) = self.tcx.optimized_mir(def_id).upvar_decls.get(field) {
563+
PathElem::ClosureVar(upvar.debug_name)
564+
} else {
565+
// Sometimes the index is beyond the number of freevars (seen
566+
// for a generator).
567+
PathElem::ClosureVar(Symbol::intern(&field.to_string()))
569568
}
570-
// The closure is not local, or the freevars don't match up (seen for a generator!),
571-
// so we cannot get the name.
572-
PathElem::ClosureVar(Symbol::intern(&field.to_string()))
573569
}
574570

575571
// tuples
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#![feature(const_transmute,const_let)]
12+
13+
use std::mem;
14+
15+
const BAD_UPVAR: &FnOnce() = &{ //~ ERROR this constant likely exhibits undefined behavior
16+
let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) };
17+
let another_var = 13;
18+
move || { let _ = bad_ref; let _ = another_var; }
19+
};
20+
21+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: this constant likely exhibits undefined behavior
2+
--> $DIR/ub-upvars.rs:15:1
3+
|
4+
LL | / const BAD_UPVAR: &FnOnce() = &{ //~ ERROR this constant likely exhibits undefined behavior
5+
LL | | let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) };
6+
LL | | let another_var = 13;
7+
LL | | move || { let _ = bad_ref; let _ = another_var; }
8+
LL | | };
9+
| |__^ type validation failed: encountered 0 at .<deref>.<closure-var(bad_ref)>, but expected something greater or equal to 1
10+
|
11+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)