about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-06-03 13:16:56 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-06-03 13:16:56 +0000
commit2e3842b6d024943dfb06c73dd15e980f6e69fcb7 (patch)
tree115670cece0f5f32a21eb16d26abebe5d0ed9e86
parent24af952ef7bf02501bf7a991c1c2feb110c77a7f (diff)
downloadrust-2e3842b6d024943dfb06c73dd15e980f6e69fcb7.tar.gz
rust-2e3842b6d024943dfb06c73dd15e980f6e69fcb7.zip
Mark all missing generic args as errors
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs39
-rw-r--r--tests/crashes/123917.rs41
-rw-r--r--tests/ui/const-generics/adt_const_params/transmutable-ice-110969.rs1
-rw-r--r--tests/ui/const-generics/adt_const_params/transmutable-ice-110969.stderr19
-rw-r--r--tests/ui/polymorphization/abi_mismatch.rs20
-rw-r--r--tests/ui/polymorphization/abi_mismatch.stderr11
6 files changed, 63 insertions, 68 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
index 7aa01b85028..a18164ec740 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@@ -422,6 +422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
             span: Span,
             inferred_params: Vec<Span>,
             infer_args: bool,
+            incorrect_args: &'a Result<(), GenericArgCountMismatch>,
         }
 
         impl<'a, 'tcx> GenericArgsLowerer<'a, 'tcx> for GenericArgsCtxt<'a, 'tcx> {
@@ -508,6 +509,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
                 infer_args: bool,
             ) -> ty::GenericArg<'tcx> {
                 let tcx = self.lowerer.tcx();
+
+                if let Err(incorrect) = self.incorrect_args {
+                    if incorrect.invalid_args.contains(&(param.index as usize)) {
+                        return match param.kind {
+                            GenericParamDefKind::Lifetime => {
+                                ty::Region::new_error(tcx, incorrect.reported).into()
+                            }
+                            GenericParamDefKind::Type { .. } => {
+                                Ty::new_error(tcx, incorrect.reported).into()
+                            }
+                            GenericParamDefKind::Const { .. } => ty::Const::new_error(
+                                tcx,
+                                incorrect.reported,
+                                Ty::new_error(tcx, incorrect.reported),
+                            )
+                            .into(),
+                        };
+                    }
+                }
                 match param.kind {
                     GenericParamDefKind::Lifetime => self
                         .lowerer
@@ -568,15 +588,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
                 }
             }
         }
-
-        let mut args_ctx = GenericArgsCtxt {
-            lowerer: self,
-            def_id,
-            span,
-            generic_args: segment.args(),
-            inferred_params: vec![],
-            infer_args: segment.infer_args,
-        };
         if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
             && generics.has_self
             && !tcx.has_attr(def_id, sym::const_trait)
@@ -588,6 +599,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
             self.set_tainted_by_errors(reported);
             arg_count.correct = Err(GenericArgCountMismatch { reported, invalid_args: vec![] });
         }
+
+        let mut args_ctx = GenericArgsCtxt {
+            lowerer: self,
+            def_id,
+            span,
+            generic_args: segment.args(),
+            inferred_params: vec![],
+            infer_args: segment.infer_args,
+            incorrect_args: &arg_count.correct,
+        };
         let args = lower_generic_args(
             tcx,
             def_id,
diff --git a/tests/crashes/123917.rs b/tests/crashes/123917.rs
deleted file mode 100644
index 66e75460662..00000000000
--- a/tests/crashes/123917.rs
+++ /dev/null
@@ -1,41 +0,0 @@
-//@ known-bug: #123917
-//@ compile-flags: -Zmir-opt-level=5 -Zpolymorphize=on
-
-use std::marker::PhantomData;
-
-pub struct Id<'id>();
-
-pub struct Item<'life, T> {
-    data: T,
-}
-
-pub struct Token<'life, 'borrow, 'compact, 'reborrow, T>
-where
-    'life: 'reborrow,
-    T: Tokenize,
-{
-    ptr: *mut <T as Tokenize>::Tokenized,
-    ptr: core::ptr::NonNull<T::Tokenized>,
-    _phantom: PhantomData<Id<'life>>,
-}
-
-impl<'life> Arena<'life> {
-    pub fn tokenize<'before, 'compact, 'borrow, 'reborrow, T, U>(
-        item: Item<'life, &'before mut T>,
-    ) -> Token<'life, 'borrow, 'compact, 'reborrow, U>
-    where
-        T: Tokenize<'life, 'borrow, 'compact, 'reborrow, Untokenized = U>,
-        T::Untokenized: Tokenize<'life, 'borrow, 'compact, 'reborrow>,
-    {
-        let dst = item.data as *mut T as *mut T::Tokenized;
-        Token {
-            ptr: core::ptr::NonNull::new(dst as *mut _).unwrap(),
-            _phantom: PhantomData,
-        }
-    }
-}
-
-pub trait Tokenize {
-    type Tokenized;
-    type Untokenized;
-}
diff --git a/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.rs b/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.rs
index 68b8b489816..569e57fa326 100644
--- a/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.rs
+++ b/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.rs
@@ -24,7 +24,6 @@ fn via_associated_const() {
     trait Trait {
         const FALSE: bool = assert::is_transmutable::<Src, Dst, Context, {}>();
         //~^ ERROR mismatched types
-        //~| ERROR `Src` cannot be safely transmuted into `Dst`
         //~| ERROR mismatched types
     }
 }
diff --git a/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.stderr b/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.stderr
index 1dbacaee3c2..a8fc742e89f 100644
--- a/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.stderr
+++ b/tests/ui/const-generics/adt_const_params/transmutable-ice-110969.stderr
@@ -12,28 +12,13 @@ error[E0308]: mismatched types
 LL |         const FALSE: bool = assert::is_transmutable::<Src, Dst, Context, {}>();
    |                                                                          ^^ expected `Assume`, found `()`
 
-error[E0277]: `Src` cannot be safely transmuted into `Dst`
-  --> $DIR/transmutable-ice-110969.rs:25:60
-   |
-LL |         const FALSE: bool = assert::is_transmutable::<Src, Dst, Context, {}>();
-   |                                                            ^^^ `Dst` may carry safety invariants
-   |
-note: required by a bound in `is_transmutable`
-  --> $DIR/transmutable-ice-110969.rs:11:14
-   |
-LL |     pub fn is_transmutable<Src, Dst, Context, const ASSUME: std::mem::Assume>()
-   |            --------------- required by a bound in this function
-LL |     where
-LL |         Dst: BikeshedIntrinsicFrom<Src, Context, ASSUME>,
-   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable`
-
 error[E0308]: mismatched types
   --> $DIR/transmutable-ice-110969.rs:25:29
    |
 LL |         const FALSE: bool = assert::is_transmutable::<Src, Dst, Context, {}>();
    |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
 
-error: aborting due to 4 previous errors
+error: aborting due to 3 previous errors
 
-Some errors have detailed explanations: E0107, E0277, E0308.
+Some errors have detailed explanations: E0107, E0308.
 For more information about an error, try `rustc --explain E0107`.
diff --git a/tests/ui/polymorphization/abi_mismatch.rs b/tests/ui/polymorphization/abi_mismatch.rs
new file mode 100644
index 00000000000..22c2c162d1c
--- /dev/null
+++ b/tests/ui/polymorphization/abi_mismatch.rs
@@ -0,0 +1,20 @@
+//! This test used to ICE: #123917
+//! The reason was that while the AST knows about two fields
+//! named `ptr`, only one exists at the layout level, so accessing
+//! `_extra_field` would use an oob index
+//@ compile-flags: -Zmir-opt-level=5 -Zpolymorphize=on
+
+struct NonNull<T>(*mut T);
+
+struct Token<T> {
+    ptr: *mut T,
+    ptr: NonNull<T>,
+    //~^ ERROR: `ptr` is already declared
+    _extra_field: (),
+}
+
+fn tokenize<T>(item: *mut T) -> Token<T> {
+    Token { ptr: NonNull(item), _extra_field: () }
+}
+
+fn main() {}
diff --git a/tests/ui/polymorphization/abi_mismatch.stderr b/tests/ui/polymorphization/abi_mismatch.stderr
new file mode 100644
index 00000000000..e96c737f777
--- /dev/null
+++ b/tests/ui/polymorphization/abi_mismatch.stderr
@@ -0,0 +1,11 @@
+error[E0124]: field `ptr` is already declared
+  --> $DIR/abi_mismatch.rs:11:5
+   |
+LL |     ptr: *mut T,
+   |     ----------- `ptr` first declared here
+LL |     ptr: NonNull<T>,
+   |     ^^^^^^^^^^^^^^^ field already declared
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0124`.