From 582d52f0bdead9f40600d468bbe8c3be7842248a Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 16 Apr 2020 17:32:48 +0200 Subject: [PATCH 1/5] Separate miri/ctfe unsupported operations --- src/librustc_middle/mir/interpret/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustc_middle/mir/interpret/error.rs b/src/librustc_middle/mir/interpret/error.rs index e6557c9fbd59d..084b5fce38589 100644 --- a/src/librustc_middle/mir/interpret/error.rs +++ b/src/librustc_middle/mir/interpret/error.rs @@ -451,6 +451,8 @@ pub enum UnsupportedOpInfo { NoMirFor(DefId), /// Encountered a pointer where we needed raw bytes. ReadPointerAsBytes, + + // The variants below are only reachable from CTFE/const prop, miri will never emit them. /// Encountered raw bytes where we needed a pointer. ReadBytesAsPointer, } From b2395a5ea6031d541ae8ee0c47caba7fdf20e19e Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 17 Apr 2020 18:55:23 +0200 Subject: [PATCH 2/5] Add a convenience function for testing whether a static is `#[thread_local]` --- src/librustc_codegen_llvm/consts.rs | 1 + src/librustc_middle/ty/util.rs | 6 ++++++ src/librustc_mir/transform/check_consts/validation.rs | 6 ++---- src/librustc_mir/transform/promote_consts.rs | 2 +- src/librustc_mir_build/build/expr/as_temp.rs | 5 ++--- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index 43ced8ee5b13c..9d9b53fc4a87c 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -212,6 +212,7 @@ impl CodegenCx<'ll, 'tcx> { let g = if let Some(def_id) = def_id.as_local() { let id = self.tcx.hir().as_local_hir_id(def_id); let llty = self.layout_of(ty).llvm_type(self); + // FIXME: refactor this to work without accessing the HIR let (g, attrs) = match self.tcx.hir().get(id) { Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => { let sym_str = sym.as_str(); diff --git a/src/librustc_middle/ty/util.rs b/src/librustc_middle/ty/util.rs index b46caf7985208..a89927ecfb72c 100644 --- a/src/librustc_middle/ty/util.rs +++ b/src/librustc_middle/ty/util.rs @@ -1,6 +1,7 @@ //! Miscellaneous type-system utilities that are too small to deserve their own modules. use crate::ich::NodeIdHashingMode; +use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; use crate::mir::interpret::{sign_extend, truncate}; use crate::ty::layout::IntegerExt; use crate::ty::query::TyCtxtAt; @@ -528,6 +529,11 @@ impl<'tcx> TyCtxt<'tcx> { self.static_mutability(def_id).is_some() } + /// Returns `true` if this is a `static` item with the `#[thread_local]` attribute. + pub fn is_thread_local_static(&self, def_id: DefId) -> bool { + self.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) + } + /// Returns `true` if the node pointed to by `def_id` is a mutable `static` item. pub fn is_mutable_static(&self, def_id: DefId) -> bool { self.static_mutability(def_id) == Some(hir::Mutability::Mut) diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs index c5938426f61a9..e7d4b5f5f91a2 100644 --- a/src/librustc_mir/transform/check_consts/validation.rs +++ b/src/librustc_mir/transform/check_consts/validation.rs @@ -8,7 +8,6 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC use rustc_middle::mir::*; use rustc_middle::ty::cast::CastTy; use rustc_middle::ty::{self, Instance, InstanceDef, TyCtxt}; -use rustc_span::symbol::sym; use rustc_span::Span; use rustc_trait_selection::traits::error_reporting::InferCtxtExt; use rustc_trait_selection::traits::{self, TraitEngine}; @@ -224,7 +223,7 @@ impl Validator<'mir, 'tcx> { // Ensure that the end result is `Sync` in a non-thread local `static`. let should_check_for_sync = - const_kind == Some(ConstKind::Static) && !tcx.has_attr(def_id, sym::thread_local); + const_kind == Some(ConstKind::Static) && !tcx.is_thread_local_static(def_id); if should_check_for_sync { let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); @@ -267,8 +266,7 @@ impl Validator<'mir, 'tcx> { } fn check_static(&mut self, def_id: DefId, span: Span) { - let is_thread_local = self.tcx.has_attr(def_id, sym::thread_local); - if is_thread_local { + if self.tcx.is_thread_local_static(def_id) { self.check_op_spanned(ops::ThreadLocalAccess, span) } else { self.check_op_spanned(ops::StaticAccess, span) diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index 0b73643e33956..ad98920eb63b9 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -522,7 +522,7 @@ impl<'tcx> Validator<'_, 'tcx> { return Err(Unpromotable); } - let is_thread_local = self.tcx.has_attr(def_id, sym::thread_local); + let is_thread_local = self.tcx.is_thread_local_static(def_id); if is_thread_local { return Err(Unpromotable); } diff --git a/src/librustc_mir_build/build/expr/as_temp.rs b/src/librustc_mir_build/build/expr/as_temp.rs index 73d95575e39d5..b4ef536afb740 100644 --- a/src/librustc_mir_build/build/expr/as_temp.rs +++ b/src/librustc_mir_build/build/expr/as_temp.rs @@ -3,10 +3,9 @@ use crate::build::scope::DropKind; use crate::build::{BlockAnd, BlockAndExtension, Builder}; use crate::hair::*; +use rustc_hir as hir; use rustc_middle::middle::region; use rustc_middle::mir::*; -use rustc_hir as hir; -use rustc_span::symbol::sym; impl<'a, 'tcx> Builder<'a, 'tcx> { /// Compile `expr` into a fresh temporary. This is used when building @@ -60,7 +59,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { local_decl = local_decl.block_tail(tail_info); } if let ExprKind::StaticRef { def_id, .. } = expr.kind { - let is_thread_local = this.hir.tcx().has_attr(def_id, sym::thread_local); + let is_thread_local = this.hir.tcx().is_thread_local_static(def_id); local_decl.internal = true; local_decl.local_info = LocalInfo::StaticRef { def_id, is_thread_local }; } From a91bad6542e40805b68fc9841fdee59a015fca2b Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 30 Apr 2020 14:53:28 +0200 Subject: [PATCH 3/5] Highlight an error that can only happen in CTFE --- src/librustc_middle/mir/interpret/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_middle/mir/interpret/error.rs b/src/librustc_middle/mir/interpret/error.rs index 084b5fce38589..15b1b7ce526a1 100644 --- a/src/librustc_middle/mir/interpret/error.rs +++ b/src/librustc_middle/mir/interpret/error.rs @@ -438,7 +438,7 @@ impl fmt::Debug for UndefinedBehaviorInfo { /// Error information for when the program did something that might (or might not) be correct /// to do according to the Rust spec, but due to limitations in the interpreter, the /// operation could not be carried out. These limitations can differ between CTFE and the -/// Miri engine, e.g., CTFE does not support casting pointers to "real" integers. +/// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses. /// /// Currently, we also use this as fall-back error kind for errors that have not been /// categorized yet. From 9cdc9321fd5af26c6d3cee40f98f799b3bc5a0c2 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 30 Apr 2020 17:05:36 +0200 Subject: [PATCH 4/5] Address review comments --- src/librustc_middle/mir/interpret/error.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/librustc_middle/mir/interpret/error.rs b/src/librustc_middle/mir/interpret/error.rs index 15b1b7ce526a1..7b222121245d6 100644 --- a/src/librustc_middle/mir/interpret/error.rs +++ b/src/librustc_middle/mir/interpret/error.rs @@ -439,9 +439,6 @@ impl fmt::Debug for UndefinedBehaviorInfo { /// to do according to the Rust spec, but due to limitations in the interpreter, the /// operation could not be carried out. These limitations can differ between CTFE and the /// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses. -/// -/// Currently, we also use this as fall-back error kind for errors that have not been -/// categorized yet. pub enum UnsupportedOpInfo { /// Free-form case. Only for errors that are never caught! Unsupported(String), @@ -451,8 +448,9 @@ pub enum UnsupportedOpInfo { NoMirFor(DefId), /// Encountered a pointer where we needed raw bytes. ReadPointerAsBytes, - + // // The variants below are only reachable from CTFE/const prop, miri will never emit them. + // /// Encountered raw bytes where we needed a pointer. ReadBytesAsPointer, } From 8079dd8afe711d736736c522626d3e6052fb0ea4 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 30 Apr 2020 17:27:33 +0200 Subject: [PATCH 5/5] A test now fails during check instead of build --- src/test/ui/linkage-attr/linkage3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/linkage-attr/linkage3.rs b/src/test/ui/linkage-attr/linkage3.rs index bd4e5ba2d4a06..e91dbf675d35f 100644 --- a/src/test/ui/linkage-attr/linkage3.rs +++ b/src/test/ui/linkage-attr/linkage3.rs @@ -1,6 +1,6 @@ // FIXME https://github.com/rust-lang/rust/issues/59774 -// build-fail +// check-fail // normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""