about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2022-10-03 17:51:18 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2022-10-03 17:51:18 +0900
commit0e615caa8dc17b5d8ca87e956d37a37364cf5994 (patch)
tree8a3a8f294ae25615bb8cae03d7778d9174fb81bb
parentb8b30ae6bab09197def6f61183715059b991faec (diff)
downloadrust-0e615caa8dc17b5d8ca87e956d37a37364cf5994.tar.gz
rust-0e615caa8dc17b5d8ca87e956d37a37364cf5994.zip
check if const is ADT or not
-rw-r--r--compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs4
-rw-r--r--compiler/rustc_trait_selection/src/traits/select/confirmation.rs13
-rw-r--r--compiler/rustc_transmute/src/lib.rs15
-rw-r--r--src/test/ui/transmutability/issue-101739-1.rs (renamed from src/test/ui/transmutability/issues-101739.rs)0
-rw-r--r--src/test/ui/transmutability/issue-101739-1.stderr (renamed from src/test/ui/transmutability/issues-101739.stderr)4
-rw-r--r--src/test/ui/transmutability/issue-101739-2.rs37
-rw-r--r--src/test/ui/transmutability/issue-101739-2.stderr20
7 files changed, 77 insertions, 16 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
index 1bca7ed8d32..451427a6980 100644
--- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
+++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
@@ -899,10 +899,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
             return;
         }
 
-        if obligation.references_error() {
-            return;
-        }
-
         candidates.vec.push(TransmutabilityCandidate);
     }
 
diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs
index dd49dcecf77..5c8a76401d2 100644
--- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs
+++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs
@@ -11,9 +11,10 @@ use rustc_hir::lang_items::LangItem;
 use rustc_index::bit_set::GrowableBitSet;
 use rustc_infer::infer::InferOk;
 use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
-use rustc_middle::ty::{self, GenericParamDefKind, Ty, TyCtxt};
-use rustc_middle::ty::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
-use rustc_middle::ty::{ToPolyTraitRef, ToPredicate};
+use rustc_middle::ty::{
+    self, GenericArg, GenericArgKind, GenericParamDefKind, InternalSubsts, SubstsRef,
+    ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
+};
 use rustc_span::def_id::DefId;
 
 use crate::traits::project::{normalize_with_depth, normalize_with_depth_to};
@@ -289,8 +290,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
 
         let scope = type_at(2).skip_binder();
 
-        let assume =
-            rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, const_at(3));
+        let Some(assume) =
+            rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, const_at(3)) else {
+                return Err(Unimplemented);
+            };
 
         let cause = obligation.cause.clone();
 
diff --git a/compiler/rustc_transmute/src/lib.rs b/compiler/rustc_transmute/src/lib.rs
index 64cd70d3678..51f2eb8606a 100644
--- a/compiler/rustc_transmute/src/lib.rs
+++ b/compiler/rustc_transmute/src/lib.rs
@@ -115,7 +115,7 @@ mod rustc {
             tcx: TyCtxt<'tcx>,
             param_env: ParamEnv<'tcx>,
             c: Const<'tcx>,
-        ) -> Self {
+        ) -> Option<Self> {
             use rustc_middle::ty::ScalarInt;
             use rustc_middle::ty::TypeVisitable;
             use rustc_span::symbol::sym;
@@ -123,10 +123,15 @@ mod rustc {
             let c = c.eval(tcx, param_env);
 
             if let Some(err) = c.error_reported() {
-                return Self { alignment: true, lifetimes: true, safety: true, validity: true };
+                return Some(Self {
+                    alignment: true,
+                    lifetimes: true,
+                    safety: true,
+                    validity: true,
+                });
             }
 
-            let adt_def = c.ty().ty_adt_def().expect("The given `Const` must be an ADT.");
+            let adt_def = c.ty().ty_adt_def()?;
 
             assert_eq!(
                 tcx.require_lang_item(LangItem::TransmuteOpts, None),
@@ -148,12 +153,12 @@ mod rustc {
                 fields[field_idx].unwrap_leaf() == ScalarInt::TRUE
             };
 
-            Self {
+            Some(Self {
                 alignment: get_field(sym::alignment),
                 lifetimes: get_field(sym::lifetimes),
                 safety: get_field(sym::safety),
                 validity: get_field(sym::validity),
-            }
+            })
         }
     }
 }
diff --git a/src/test/ui/transmutability/issues-101739.rs b/src/test/ui/transmutability/issue-101739-1.rs
index bcb8b158edf..bcb8b158edf 100644
--- a/src/test/ui/transmutability/issues-101739.rs
+++ b/src/test/ui/transmutability/issue-101739-1.rs
diff --git a/src/test/ui/transmutability/issues-101739.stderr b/src/test/ui/transmutability/issue-101739-1.stderr
index 502c9751b54..5fa741f26fd 100644
--- a/src/test/ui/transmutability/issues-101739.stderr
+++ b/src/test/ui/transmutability/issue-101739-1.stderr
@@ -1,11 +1,11 @@
 error[E0412]: cannot find type `Dst` in this scope
-  --> $DIR/issues-101739.rs:8:9
+  --> $DIR/issue-101739-1.rs:8:9
    |
 LL |         Dst: BikeshedIntrinsicFrom<Src, Context, ASSUME_ALIGNMENT>,
    |         ^^^ not found in this scope
 
 error[E0308]: mismatched types
-  --> $DIR/issues-101739.rs:8:50
+  --> $DIR/issue-101739-1.rs:8:50
    |
 LL |         Dst: BikeshedIntrinsicFrom<Src, Context, ASSUME_ALIGNMENT>,
    |                                                  ^^^^^^^^^^^^^^^^ expected struct `Assume`, found `bool`
diff --git a/src/test/ui/transmutability/issue-101739-2.rs b/src/test/ui/transmutability/issue-101739-2.rs
new file mode 100644
index 00000000000..964a7e49ee6
--- /dev/null
+++ b/src/test/ui/transmutability/issue-101739-2.rs
@@ -0,0 +1,37 @@
+#![crate_type = "lib"]
+#![feature(transmutability)]
+#![allow(dead_code, incomplete_features, non_camel_case_types)]
+
+mod assert {
+    use std::mem::BikeshedIntrinsicFrom;
+
+    pub fn is_transmutable<
+        Src,
+        Dst,
+        Context,
+        const ASSUME_ALIGNMENT: bool,
+        const ASSUME_LIFETIMES: bool,
+        const ASSUME_VALIDITY: bool,
+        const ASSUME_VISIBILITY: bool,
+    >()
+    where
+        Dst: BikeshedIntrinsicFrom< //~ ERROR this trait takes at most 3 generic arguments but 6 generic arguments were supplied
+            Src,
+            Context,
+            ASSUME_ALIGNMENT,
+            ASSUME_LIFETIMES,
+            ASSUME_VALIDITY,
+            ASSUME_VISIBILITY,
+        >,
+    {}
+}
+
+fn via_const() {
+    struct Context;
+    #[repr(C)] struct Src;
+    #[repr(C)] struct Dst;
+
+    const FALSE: bool = false;
+
+    assert::is_transmutable::<Src, Dst, Context, FALSE, FALSE, FALSE, FALSE>();
+}
diff --git a/src/test/ui/transmutability/issue-101739-2.stderr b/src/test/ui/transmutability/issue-101739-2.stderr
new file mode 100644
index 00000000000..3f83d6583b0
--- /dev/null
+++ b/src/test/ui/transmutability/issue-101739-2.stderr
@@ -0,0 +1,20 @@
+error[E0107]: this trait takes at most 3 generic arguments but 6 generic arguments were supplied
+  --> $DIR/issue-101739-2.rs:18:14
+   |
+LL |           Dst: BikeshedIntrinsicFrom<
+   |                ^^^^^^^^^^^^^^^^^^^^^ expected at most 3 generic arguments
+...
+LL | /             ASSUME_LIFETIMES,
+LL | |             ASSUME_VALIDITY,
+LL | |             ASSUME_VISIBILITY,
+   | |_____________________________- help: remove these generic arguments
+   |
+note: trait defined here, with at most 3 generic parameters: `Src`, `Context`, `ASSUME`
+  --> $SRC_DIR/core/src/mem/transmutability.rs:LL:COL
+   |
+LL | pub unsafe trait BikeshedIntrinsicFrom<Src, Context, const ASSUME: Assume = { Assume::NOTHING }>
+   |                  ^^^^^^^^^^^^^^^^^^^^^ ---  -------  ------------------------------------------
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0107`.