From c96eb706f0ddee1357f5b1d81683746390ef1d01 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 11 Oct 2018 10:16:51 +0200 Subject: [PATCH] Fix and test upvar name printing for validity --- src/librustc_mir/interpret/validity.rs | 16 ++++++-------- src/test/ui/consts/const-eval/ub-upvars.rs | 21 +++++++++++++++++++ .../ui/consts/const-eval/ub-upvars.stderr | 15 +++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/consts/const-eval/ub-upvars.rs create mode 100644 src/test/ui/consts/const-eval/ub-upvars.stderr diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 59df61f2a6f..fb81ab4e8c4 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -559,17 +559,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> match layout.ty.sty { // generators and closures. ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => { - if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) { - if let Some(freevar) = self.tcx.with_freevars( - node_id, - |fv| fv.get(field).map(|field| *field)) - { - return PathElem::ClosureVar(self.tcx.hir.name(freevar.var_id())); - } + if let Some(upvar) = self.tcx.optimized_mir(def_id).upvar_decls.get(field) { + PathElem::ClosureVar(upvar.debug_name) + } else { + // Sometimes the index is beyond the number of freevars (seen + // for a generator). + PathElem::ClosureVar(Symbol::intern(&field.to_string())) } - // The closure is not local, or the freevars don't match up (seen for a generator!), - // so we cannot get the name. - PathElem::ClosureVar(Symbol::intern(&field.to_string())) } // tuples diff --git a/src/test/ui/consts/const-eval/ub-upvars.rs b/src/test/ui/consts/const-eval/ub-upvars.rs new file mode 100644 index 00000000000..309211d19d4 --- /dev/null +++ b/src/test/ui/consts/const-eval/ub-upvars.rs @@ -0,0 +1,21 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_transmute,const_let)] + +use std::mem; + +const BAD_UPVAR: &FnOnce() = &{ //~ ERROR this constant likely exhibits undefined behavior + let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) }; + let another_var = 13; + move || { let _ = bad_ref; let _ = another_var; } +}; + +fn main() {} diff --git a/src/test/ui/consts/const-eval/ub-upvars.stderr b/src/test/ui/consts/const-eval/ub-upvars.stderr new file mode 100644 index 00000000000..3ae140d6e1c --- /dev/null +++ b/src/test/ui/consts/const-eval/ub-upvars.stderr @@ -0,0 +1,15 @@ +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/ub-upvars.rs:15:1 + | +LL | / const BAD_UPVAR: &FnOnce() = &{ //~ ERROR this constant likely exhibits undefined behavior +LL | | let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) }; +LL | | let another_var = 13; +LL | | move || { let _ = bad_ref; let _ = another_var; } +LL | | }; + | |__^ type validation failed: encountered 0 at .., but expected something greater or equal to 1 + | + = 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 + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`.