From f208f207d647336f453b1374e2b2f96fc0548f76 Mon Sep 17 00:00:00 2001 From: Ellen Date: Sat, 29 May 2021 03:54:32 +0100 Subject: [PATCH 01/10] Make lifetime ordering error pretty print const param defaults --- compiler/rustc_ast_passes/src/ast_validation.rs | 9 ++++++--- .../defaults/intermixed-lifetime.min.stderr | 4 ++-- .../defaults/param-order-err-pretty-prints-default.rs | 5 +++++ .../param-order-err-pretty-prints-default.stderr | 8 ++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs create mode 100644 src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index ba2da7694978d..30aa51a121a80 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -938,8 +938,11 @@ fn validate_generic_param_order( } GenericParamKind::Type { default: None } => (), GenericParamKind::Lifetime => (), - // FIXME(const_generics_defaults) - GenericParamKind::Const { ty: _, kw_span: _, default: _ } => (), + GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => { + ordered_params += " = "; + ordered_params += &pprust::expr_to_string(&*default.value); + } + GenericParamKind::Const { ty: _, kw_span: _, default: None } => (), } first = false; } @@ -959,7 +962,7 @@ fn validate_generic_param_order( span, &format!( "reorder the parameters: lifetimes, {}", - if sess.features_untracked().const_generics { + if sess.features_untracked().unordered_const_ty_params() { "then consts and types" } else { "then types, then consts" diff --git a/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr b/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr index 985e7b655ece9..29d835e36c6eb 100644 --- a/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr +++ b/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr @@ -2,13 +2,13 @@ error: lifetime parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:7:28 | LL | struct Foo(&'a (), T); - | -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>` + | -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` error: lifetime parameters must be declared prior to type parameters --> $DIR/intermixed-lifetime.rs:10:37 | LL | struct Bar(&'a (), T); - | --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>` + | --------------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs new file mode 100644 index 0000000000000..933eacb312dbf --- /dev/null +++ b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs @@ -0,0 +1,5 @@ +#![feature(const_generics_defaults)] +struct Foo(&'a u32); +//~^ Error lifetime parameters must be declared prior to const parameters + +fn main() {} diff --git a/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr new file mode 100644 index 0000000000000..f50653fe9a19f --- /dev/null +++ b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr @@ -0,0 +1,8 @@ +error: lifetime parameters must be declared prior to const parameters + --> $DIR/param-order-err-pretty-prints-default.rs:2:33 + | +LL | struct Foo(&'a u32); + | ----------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const M: usize = 10>` + +error: aborting due to previous error + From d75742b1eb42f0f5c1be42fc68a88c1444b4fb57 Mon Sep 17 00:00:00 2001 From: Ellen Date: Sat, 29 May 2021 05:37:45 +0100 Subject: [PATCH 02/10] Fix missing note on type mismatch error diagnostics --- compiler/rustc_middle/src/ty/diagnostics.rs | 2 +- src/test/ui/const-generics/defaults/mismatch.full.stderr | 8 +++++++- src/test/ui/const-generics/defaults/mismatch.min.stderr | 8 +++++++- src/test/ui/const-generics/defaults/mismatch.rs | 2 -- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 982c8a354b4ab..866df2958a02a 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -54,7 +54,7 @@ impl<'tcx> TyS<'tcx> { /// ADTs with no type arguments. pub fn is_simple_text(&self) -> bool { match self.kind() { - Adt(_, substs) => substs.types().next().is_none(), + Adt(_, substs) => substs.types().next().is_none() && substs.consts().next().is_none(), Ref(_, ty, _) => ty.is_simple_text(), _ => self.is_simple_ty(), } diff --git a/src/test/ui/const-generics/defaults/mismatch.full.stderr b/src/test/ui/const-generics/defaults/mismatch.full.stderr index be4f364d8ee62..741137afe4588 100644 --- a/src/test/ui/const-generics/defaults/mismatch.full.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.full.stderr @@ -5,6 +5,9 @@ LL | let e: Example::<13> = (); | ------------- ^^ expected struct `Example`, found `()` | | | expected due to this + | + = note: expected struct `Example` + found unit type `()` error[E0308]: mismatched types --> $DIR/mismatch.rs:14:34 @@ -40,12 +43,15 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:22:28 + --> $DIR/mismatch.rs:20:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` | | | expected due to this + | + = note: expected struct `Example4<7_usize>` + found unit type `()` error: aborting due to 5 previous errors diff --git a/src/test/ui/const-generics/defaults/mismatch.min.stderr b/src/test/ui/const-generics/defaults/mismatch.min.stderr index be4f364d8ee62..741137afe4588 100644 --- a/src/test/ui/const-generics/defaults/mismatch.min.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.min.stderr @@ -5,6 +5,9 @@ LL | let e: Example::<13> = (); | ------------- ^^ expected struct `Example`, found `()` | | | expected due to this + | + = note: expected struct `Example` + found unit type `()` error[E0308]: mismatched types --> $DIR/mismatch.rs:14:34 @@ -40,12 +43,15 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:22:28 + --> $DIR/mismatch.rs:20:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` | | | expected due to this + | + = note: expected struct `Example4<7_usize>` + found unit type `()` error: aborting due to 5 previous errors diff --git a/src/test/ui/const-generics/defaults/mismatch.rs b/src/test/ui/const-generics/defaults/mismatch.rs index 68a640c0a08b3..cfc8594f17cfa 100644 --- a/src/test/ui/const-generics/defaults/mismatch.rs +++ b/src/test/ui/const-generics/defaults/mismatch.rs @@ -17,8 +17,6 @@ fn main() { //~^ Error: mismatched types let e: Example3::<7> = (); //~^ Error: mismatched types - // FIXME(const_generics_defaults): There should be a note for the error below, but it is - // missing. let e: Example4::<7> = (); //~^ Error: mismatched types } From 0eda5098bd0931a22cb42930d0963306c92f15f2 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 30 May 2021 10:54:50 +0800 Subject: [PATCH 03/10] Do not suggest ampmut if rhs is already mutable --- .../borrow_check/diagnostics/mutability_errors.rs | 8 ++++++-- src/test/ui/borrowck/issue-85765.rs | 8 ++++++++ src/test/ui/borrowck/issue-85765.stderr | 12 ++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/borrowck/issue-85765.rs create mode 100644 src/test/ui/borrowck/issue-85765.stderr diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs index d2b156610476c..bf5f2c0eec23e 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs @@ -902,9 +902,13 @@ fn suggest_ampmut<'tcx>( { let lt_name = &src[1..ws_pos]; let ty = &src[ws_pos..]; - return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty)); + if !ty.trim_start().starts_with("mut") { + return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty)); + } } else if let Some(stripped) = src.strip_prefix('&') { - return (assignment_rhs_span, format!("&mut {}", stripped)); + if !stripped.trim_start().starts_with("mut") { + return (assignment_rhs_span, format!("&mut {}", stripped)); + } } } } diff --git a/src/test/ui/borrowck/issue-85765.rs b/src/test/ui/borrowck/issue-85765.rs new file mode 100644 index 0000000000000..b82e0298158aa --- /dev/null +++ b/src/test/ui/borrowck/issue-85765.rs @@ -0,0 +1,8 @@ +fn main() { + let mut test = Vec::new(); + let rofl: &Vec> = &mut test; + //~^ HELP consider changing this to be a mutable reference + rofl.push(Vec::new()); + //~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference + //~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable +} diff --git a/src/test/ui/borrowck/issue-85765.stderr b/src/test/ui/borrowck/issue-85765.stderr new file mode 100644 index 0000000000000..863c2e8eccc8c --- /dev/null +++ b/src/test/ui/borrowck/issue-85765.stderr @@ -0,0 +1,12 @@ +error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference + --> $DIR/issue-85765.rs:5:5 + | +LL | let rofl: &Vec> = &mut test; + | ---- help: consider changing this to be a mutable reference: `&mut Vec>` +LL | +LL | rofl.push(Vec::new()); + | ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0596`. From 21078c140fcb83c4232a8e7c6ad6707c3f10cdcb Mon Sep 17 00:00:00 2001 From: Ellen Date: Mon, 7 Jun 2021 10:09:47 +0100 Subject: [PATCH 04/10] note :sparkles: uwuuu --- src/test/ui/const-generics/defaults/mismatch.full.stderr | 8 ++++---- src/test/ui/const-generics/defaults/mismatch.min.stderr | 8 ++++---- src/test/ui/const-generics/defaults/mismatch.rs | 5 +++++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/test/ui/const-generics/defaults/mismatch.full.stderr b/src/test/ui/const-generics/defaults/mismatch.full.stderr index 741137afe4588..4aa8401ab2216 100644 --- a/src/test/ui/const-generics/defaults/mismatch.full.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.full.stderr @@ -10,7 +10,7 @@ LL | let e: Example::<13> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:14:34 + --> $DIR/mismatch.rs:15:34 | LL | let e: Example2:: = (); | ------------------- ^^ expected struct `Example2`, found `()` @@ -21,7 +21,7 @@ LL | let e: Example2:: = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:16:34 + --> $DIR/mismatch.rs:18:34 | LL | let e: Example3::<13, u32> = (); | ------------------- ^^ expected struct `Example3`, found `()` @@ -32,7 +32,7 @@ LL | let e: Example3::<13, u32> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:18:28 + --> $DIR/mismatch.rs:21:28 | LL | let e: Example3::<7> = (); | ------------- ^^ expected struct `Example3`, found `()` @@ -43,7 +43,7 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:20:28 + --> $DIR/mismatch.rs:24:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` diff --git a/src/test/ui/const-generics/defaults/mismatch.min.stderr b/src/test/ui/const-generics/defaults/mismatch.min.stderr index 741137afe4588..4aa8401ab2216 100644 --- a/src/test/ui/const-generics/defaults/mismatch.min.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.min.stderr @@ -10,7 +10,7 @@ LL | let e: Example::<13> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:14:34 + --> $DIR/mismatch.rs:15:34 | LL | let e: Example2:: = (); | ------------------- ^^ expected struct `Example2`, found `()` @@ -21,7 +21,7 @@ LL | let e: Example2:: = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:16:34 + --> $DIR/mismatch.rs:18:34 | LL | let e: Example3::<13, u32> = (); | ------------------- ^^ expected struct `Example3`, found `()` @@ -32,7 +32,7 @@ LL | let e: Example3::<13, u32> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:18:28 + --> $DIR/mismatch.rs:21:28 | LL | let e: Example3::<7> = (); | ------------- ^^ expected struct `Example3`, found `()` @@ -43,7 +43,7 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:20:28 + --> $DIR/mismatch.rs:24:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` diff --git a/src/test/ui/const-generics/defaults/mismatch.rs b/src/test/ui/const-generics/defaults/mismatch.rs index cfc8594f17cfa..9d9a8793aaac0 100644 --- a/src/test/ui/const-generics/defaults/mismatch.rs +++ b/src/test/ui/const-generics/defaults/mismatch.rs @@ -11,12 +11,17 @@ pub struct Example4; fn main() { let e: Example::<13> = (); //~^ Error: mismatched types + //~| expected struct `Example` let e: Example2:: = (); //~^ Error: mismatched types + //~| expected struct `Example2` let e: Example3::<13, u32> = (); //~^ Error: mismatched types + //~| expected struct `Example3` let e: Example3::<7> = (); //~^ Error: mismatched types + //~| expected struct `Example3<7_usize>` let e: Example4::<7> = (); //~^ Error: mismatched types + //~| expected struct `Example4<7_usize>` } From 47fe696d8f247d91c67068c25bb91767713aeb60 Mon Sep 17 00:00:00 2001 From: Ellen Date: Tue, 8 Jun 2021 09:07:52 +0100 Subject: [PATCH 05/10] use non_erasable_generics --- compiler/rustc_middle/src/ty/diagnostics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 866df2958a02a..bfb4c0cb538de 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -54,7 +54,7 @@ impl<'tcx> TyS<'tcx> { /// ADTs with no type arguments. pub fn is_simple_text(&self) -> bool { match self.kind() { - Adt(_, substs) => substs.types().next().is_none() && substs.consts().next().is_none(), + Adt(_, substs) => substs.non_erasable_generics().next().is_none(), Ref(_, ty, _) => ty.is_simple_text(), _ => self.is_simple_ty(), } From fab319be73bac3773eb467139501b89b6ae3d13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Wed, 9 Jun 2021 00:00:00 +0000 Subject: [PATCH 06/10] Print dummy spans as `no-location` --- compiler/rustc_span/src/source_map.rs | 2 +- ...dead_unwind.main-{closure#0}.StateTransform.before.mir | 4 ++-- .../loop_test.main.SimplifyCfg-promote-consts.after.mir | 2 +- ...ch.SimplifyCfg-initial.after-ElaborateDrops.after.diff | 8 ++++---- .../mir-opt/while_storage.while_loop.PreCodegen.after.mir | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 0dadd4192004f..1d45cd172b300 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -407,7 +407,7 @@ impl SourceMap { } fn span_to_string(&self, sp: Span, prefer_local: bool) -> String { - if self.files.borrow().source_files.is_empty() && sp.is_dummy() { + if self.files.borrow().source_files.is_empty() || sp.is_dummy() { return "no-location".to_string(); } diff --git a/src/test/mir-opt/generator_storage_dead_unwind.main-{closure#0}.StateTransform.before.mir b/src/test/mir-opt/generator_storage_dead_unwind.main-{closure#0}.StateTransform.before.mir index 42b95b5c68c48..296a459b99f40 100644 --- a/src/test/mir-opt/generator_storage_dead_unwind.main-{closure#0}.StateTransform.before.mir +++ b/src/test/mir-opt/generator_storage_dead_unwind.main-{closure#0}.StateTransform.before.mir @@ -85,13 +85,13 @@ yields () bb8 (cleanup): { StorageDead(_10); // scope 2 at $DIR/generator-storage-dead-unwind.rs:27:15: 27:16 StorageDead(_9); // scope 2 at $DIR/generator-storage-dead-unwind.rs:27:16: 27:17 - goto -> bb10; // scope 2 at $DIR/generator-storage-dead-unwind.rs:1:1: 1:1 + goto -> bb10; // scope 2 at no-location } bb9 (cleanup): { StorageDead(_8); // scope 2 at $DIR/generator-storage-dead-unwind.rs:26:15: 26:16 StorageDead(_7); // scope 2 at $DIR/generator-storage-dead-unwind.rs:26:16: 26:17 - goto -> bb10; // scope 2 at $DIR/generator-storage-dead-unwind.rs:1:1: 1:1 + goto -> bb10; // scope 2 at no-location } bb10 (cleanup): { diff --git a/src/test/mir-opt/loop_test.main.SimplifyCfg-promote-consts.after.mir b/src/test/mir-opt/loop_test.main.SimplifyCfg-promote-consts.after.mir index 99c7ac8d5b708..db88f77bb630e 100644 --- a/src/test/mir-opt/loop_test.main.SimplifyCfg-promote-consts.after.mir +++ b/src/test/mir-opt/loop_test.main.SimplifyCfg-promote-consts.after.mir @@ -43,7 +43,7 @@ fn main() -> () { _6 = const 1_i32; // scope 0 at $DIR/loop_test.rs:14:17: 14:18 FakeRead(ForLet(None), _6); // scope 0 at $DIR/loop_test.rs:14:13: 14:14 StorageDead(_6); // scope 0 at $DIR/loop_test.rs:16:5: 16:6 - goto -> bb3; // scope 0 at $DIR/loop_test.rs:1:1: 1:1 + goto -> bb3; // scope 0 at no-location } bb5 (cleanup): { diff --git a/src/test/mir-opt/match_arm_scopes.complicated_match.SimplifyCfg-initial.after-ElaborateDrops.after.diff b/src/test/mir-opt/match_arm_scopes.complicated_match.SimplifyCfg-initial.after-ElaborateDrops.after.diff index 3395cbfbdfb3a..feb25035ee0ec 100644 --- a/src/test/mir-opt/match_arm_scopes.complicated_match.SimplifyCfg-initial.after-ElaborateDrops.after.diff +++ b/src/test/mir-opt/match_arm_scopes.complicated_match.SimplifyCfg-initial.after-ElaborateDrops.after.diff @@ -94,8 +94,8 @@ _0 = const 3_i32; // scope 0 at $DIR/match-arm-scopes.rs:15:59: 15:60 StorageDead(_10); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73 StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78 -- goto -> bb23; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1 -+ goto -> bb20; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1 +- goto -> bb23; // scope 0 at no-location ++ goto -> bb20; // scope 0 at no-location } - bb10: { @@ -150,8 +150,8 @@ _0 = const 3_i32; // scope 0 at $DIR/match-arm-scopes.rs:15:59: 15:60 StorageDead(_13); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73 StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78 -- goto -> bb23; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1 -+ goto -> bb20; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1 +- goto -> bb23; // scope 0 at no-location ++ goto -> bb20; // scope 0 at no-location } - bb15: { diff --git a/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir b/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir index b91aae054de9d..bf9c2d138a0f6 100644 --- a/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir +++ b/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir @@ -40,7 +40,7 @@ fn while_loop(_1: bool) -> () { bb4: { StorageDead(_4); // scope 0 at $DIR/while-storage.rs:13:9: 13:10 - goto -> bb6; // scope 0 at $DIR/while-storage.rs:1:1: 1:1 + goto -> bb6; // scope 0 at no-location } bb5: { From d44990367d57e40a6ec157174a463818665ac6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Tue, 8 Jun 2021 23:47:04 +0200 Subject: [PATCH 07/10] detect incorrect vtable alignment during const eval instead of ICE-ing also add tests for these 2 kinds of errors for size and alignment, as the existing size check wasn't apparently tested --- compiler/rustc_mir/src/interpret/traits.rs | 4 ++- .../consts/const-eval/ub-incorrect-vtable.rs | 21 +++++++++++++++ .../const-eval/ub-incorrect-vtable.stderr | 27 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/consts/const-eval/ub-incorrect-vtable.rs create mode 100644 src/test/ui/consts/const-eval/ub-incorrect-vtable.stderr diff --git a/compiler/rustc_mir/src/interpret/traits.rs b/compiler/rustc_mir/src/interpret/traits.rs index 11f8d388820e8..d0c04b5b414eb 100644 --- a/compiler/rustc_mir/src/interpret/traits.rs +++ b/compiler/rustc_mir/src/interpret/traits.rs @@ -158,6 +158,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let size = u64::try_from(self.force_bits(size, pointer_size)?).unwrap(); let align = vtable.read_ptr_sized(pointer_size * 2)?.check_init()?; let align = u64::try_from(self.force_bits(align, pointer_size)?).unwrap(); + let align = Align::from_bytes(align) + .map_err(|e| err_ub_format!("invalid vtable: alignment {}", e))?; if size >= self.tcx.data_layout.obj_size_bound() { throw_ub_format!( @@ -165,6 +167,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { size is bigger than largest supported object" ); } - Ok((Size::from_bytes(size), Align::from_bytes(align).unwrap())) + Ok((Size::from_bytes(size), align)) } } diff --git a/src/test/ui/consts/const-eval/ub-incorrect-vtable.rs b/src/test/ui/consts/const-eval/ub-incorrect-vtable.rs new file mode 100644 index 0000000000000..0c0e3682de4d6 --- /dev/null +++ b/src/test/ui/consts/const-eval/ub-incorrect-vtable.rs @@ -0,0 +1,21 @@ +// This test contains code with incorrect vtables in a const context: +// - from issue 86132: a trait object with invalid alignment caused an ICE in const eval, and now +// triggers an error +// - a similar test that triggers a previously-untested const UB error: emitted close to the above +// error, it checks the correctness of the size + +trait Trait {} + +const INVALID_VTABLE_ALIGNMENT: &dyn Trait = + unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) }; +//~^ ERROR any use of this value will cause an error +//~| WARNING this was previously accepted by the compiler +//~| invalid vtable: alignment `1000` is not a power of 2 + +const INVALID_VTABLE_SIZE: &dyn Trait = + unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) }; +//~^ ERROR any use of this value will cause an error +//~| WARNING this was previously accepted by the compiler +//~| invalid vtable: size is bigger than largest supported object + +fn main() {} diff --git a/src/test/ui/consts/const-eval/ub-incorrect-vtable.stderr b/src/test/ui/consts/const-eval/ub-incorrect-vtable.stderr new file mode 100644 index 0000000000000..c937d039d381e --- /dev/null +++ b/src/test/ui/consts/const-eval/ub-incorrect-vtable.stderr @@ -0,0 +1,27 @@ +error: any use of this value will cause an error + --> $DIR/ub-incorrect-vtable.rs:10:14 + | +LL | / const INVALID_VTABLE_ALIGNMENT: &dyn Trait = +LL | | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) }; + | |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__- + | | + | invalid vtable: alignment `1000` is not a power of 2 + | + = note: `#[deny(const_err)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #71800 + +error: any use of this value will cause an error + --> $DIR/ub-incorrect-vtable.rs:16:14 + | +LL | / const INVALID_VTABLE_SIZE: &dyn Trait = +LL | | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) }; + | |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__- + | | + | invalid vtable: size is bigger than largest supported object + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #71800 + +error: aborting due to 2 previous errors + From 360785775647e25079bc920d4ad40a688760798a Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 10 Jun 2021 11:48:51 +0900 Subject: [PATCH 08/10] Make `relate_type_and_mut` public --- compiler/rustc_middle/src/ty/relate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 872d12cac9384..3f426b13688fe 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -112,7 +112,7 @@ pub trait Relate<'tcx>: TypeFoldable<'tcx> + Copy { /////////////////////////////////////////////////////////////////////////// // Relate impls -fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>( +pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>( relation: &mut R, a: ty::TypeAndMut<'tcx>, b: ty::TypeAndMut<'tcx>, From d7e0f431dedbf3684496ef1f2289ab1c7d936e60 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 11 Jun 2021 06:20:54 +0900 Subject: [PATCH 09/10] Run full const-generics test for issue-72293 --- .../transmute-const-param-static-reference.rs | 10 ---------- ...mute-const-param-static-reference.min.stderr} | 2 +- .../transmute-const-param-static-reference.rs | 16 ++++++++++++++++ 3 files changed, 17 insertions(+), 11 deletions(-) delete mode 100644 src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.rs rename src/test/ui/const-generics/{min_const_generics/transmute-const-param-static-reference.stderr => transmute-const-param-static-reference.min.stderr} (85%) create mode 100644 src/test/ui/const-generics/transmute-const-param-static-reference.rs diff --git a/src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.rs b/src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.rs deleted file mode 100644 index 560795a51f58e..0000000000000 --- a/src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.rs +++ /dev/null @@ -1,10 +0,0 @@ -struct Const; -//~^ ERROR `&'static ()` is forbidden as the type of a const generic parameter - -fn main() { - const A: &'static () = unsafe { - std::mem::transmute(10 as *const ()) - }; - - let _ = Const::<{A}>; -} diff --git a/src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.stderr b/src/test/ui/const-generics/transmute-const-param-static-reference.min.stderr similarity index 85% rename from src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.stderr rename to src/test/ui/const-generics/transmute-const-param-static-reference.min.stderr index d612e0c35a10f..f735be9c24e08 100644 --- a/src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.stderr +++ b/src/test/ui/const-generics/transmute-const-param-static-reference.min.stderr @@ -1,5 +1,5 @@ error: `&'static ()` is forbidden as the type of a const generic parameter - --> $DIR/transmute-const-param-static-reference.rs:1:23 + --> $DIR/transmute-const-param-static-reference.rs:7:23 | LL | struct Const; | ^^^^^^^^^^^ diff --git a/src/test/ui/const-generics/transmute-const-param-static-reference.rs b/src/test/ui/const-generics/transmute-const-param-static-reference.rs new file mode 100644 index 0000000000000..3147d61ec9bd9 --- /dev/null +++ b/src/test/ui/const-generics/transmute-const-param-static-reference.rs @@ -0,0 +1,16 @@ +// revisions: full min +//[full] check-pass + +#![cfg_attr(full, feature(const_generics))] +#![cfg_attr(full, allow(incomplete_features))] + +struct Const; +//[min]~^ ERROR `&'static ()` is forbidden as the type of a const generic parameter + +fn main() { + const A: &'static () = unsafe { + std::mem::transmute(10 as *const ()) + }; + + let _ = Const::<{A}>; +} From 8f78660c82689364035b7aace8b3ef5ca27e0921 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sat, 12 Jun 2021 04:11:48 +0800 Subject: [PATCH 10/10] Remove "generic type" in boxed.rs --- library/alloc/src/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index eb91af8c61cbc..13b42442dcf09 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -1209,7 +1209,7 @@ impl Hasher for Box { #[cfg(not(no_global_oom_handling))] #[stable(feature = "from_for_ptrs", since = "1.6.0")] impl From for Box { - /// Converts a generic type `T` into a `Box` + /// Converts a `T` into a `Box` /// /// The conversion allocates on the heap and moves `t` /// from the stack into it.