Skip to content

Commit 3198c52

Browse files
committed
Auto merge of #86226 - JohnTitor:rollup-5ubdolf, r=JohnTitor
Rollup of 7 pull requests Successful merges: - #85800 (Fix some diagnostic issues with const_generics_defaults feature gate) - #85823 (Do not suggest ampmut if rhs is already mutable) - #86153 (Print dummy spans as `no-location`) - #86174 (Detect incorrect vtable alignment during const eval) - #86189 (Make `relate_type_and_mut` public) - #86205 (Run full const-generics test for issue-72293) - #86217 (Remove "generic type" in boxed.rs) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0a8629b + e13b53e commit 3198c52

24 files changed

+152
-41
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,11 @@ fn validate_generic_param_order(
938938
}
939939
GenericParamKind::Type { default: None } => (),
940940
GenericParamKind::Lifetime => (),
941-
// FIXME(const_generics_defaults)
942-
GenericParamKind::Const { ty: _, kw_span: _, default: _ } => (),
941+
GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => {
942+
ordered_params += " = ";
943+
ordered_params += &pprust::expr_to_string(&*default.value);
944+
}
945+
GenericParamKind::Const { ty: _, kw_span: _, default: None } => (),
943946
}
944947
first = false;
945948
}
@@ -959,7 +962,7 @@ fn validate_generic_param_order(
959962
span,
960963
&format!(
961964
"reorder the parameters: lifetimes, {}",
962-
if sess.features_untracked().const_generics {
965+
if sess.features_untracked().unordered_const_ty_params() {
963966
"then consts and types"
964967
} else {
965968
"then types, then consts"

compiler/rustc_middle/src/ty/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> TyS<'tcx> {
5454
/// ADTs with no type arguments.
5555
pub fn is_simple_text(&self) -> bool {
5656
match self.kind() {
57-
Adt(_, substs) => substs.types().next().is_none(),
57+
Adt(_, substs) => substs.non_erasable_generics().next().is_none(),
5858
Ref(_, ty, _) => ty.is_simple_text(),
5959
_ => self.is_simple_ty(),
6060
}

compiler/rustc_middle/src/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub trait Relate<'tcx>: TypeFoldable<'tcx> + Copy {
112112
///////////////////////////////////////////////////////////////////////////
113113
// Relate impls
114114

115-
fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>(
115+
pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>(
116116
relation: &mut R,
117117
a: ty::TypeAndMut<'tcx>,
118118
b: ty::TypeAndMut<'tcx>,

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,13 @@ fn suggest_ampmut<'tcx>(
902902
{
903903
let lt_name = &src[1..ws_pos];
904904
let ty = &src[ws_pos..];
905-
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
905+
if !ty.trim_start().starts_with("mut") {
906+
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
907+
}
906908
} else if let Some(stripped) = src.strip_prefix('&') {
907-
return (assignment_rhs_span, format!("&mut {}", stripped));
909+
if !stripped.trim_start().starts_with("mut") {
910+
return (assignment_rhs_span, format!("&mut {}", stripped));
911+
}
908912
}
909913
}
910914
}

compiler/rustc_mir/src/interpret/traits.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
158158
let size = u64::try_from(self.force_bits(size, pointer_size)?).unwrap();
159159
let align = vtable.read_ptr_sized(pointer_size * 2)?.check_init()?;
160160
let align = u64::try_from(self.force_bits(align, pointer_size)?).unwrap();
161+
let align = Align::from_bytes(align)
162+
.map_err(|e| err_ub_format!("invalid vtable: alignment {}", e))?;
161163

162164
if size >= self.tcx.data_layout.obj_size_bound() {
163165
throw_ub_format!(
164166
"invalid vtable: \
165167
size is bigger than largest supported object"
166168
);
167169
}
168-
Ok((Size::from_bytes(size), Align::from_bytes(align).unwrap()))
170+
Ok((Size::from_bytes(size), align))
169171
}
170172
}

compiler/rustc_span/src/source_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl SourceMap {
407407
}
408408

409409
fn span_to_string(&self, sp: Span, prefer_local: bool) -> String {
410-
if self.files.borrow().source_files.is_empty() && sp.is_dummy() {
410+
if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
411411
return "no-location".to_string();
412412
}
413413

library/alloc/src/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ impl<T: ?Sized + Hasher, A: Allocator> Hasher for Box<T, A> {
12091209
#[cfg(not(no_global_oom_handling))]
12101210
#[stable(feature = "from_for_ptrs", since = "1.6.0")]
12111211
impl<T> From<T> for Box<T> {
1212-
/// Converts a generic type `T` into a `Box<T>`
1212+
/// Converts a `T` into a `Box<T>`
12131213
///
12141214
/// The conversion allocates on the heap and moves `t`
12151215
/// from the stack into it.

src/test/mir-opt/generator_storage_dead_unwind.main-{closure#0}.StateTransform.before.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ yields ()
8585
bb8 (cleanup): {
8686
StorageDead(_10); // scope 2 at $DIR/generator-storage-dead-unwind.rs:27:15: 27:16
8787
StorageDead(_9); // scope 2 at $DIR/generator-storage-dead-unwind.rs:27:16: 27:17
88-
goto -> bb10; // scope 2 at $DIR/generator-storage-dead-unwind.rs:1:1: 1:1
88+
goto -> bb10; // scope 2 at no-location
8989
}
9090

9191
bb9 (cleanup): {
9292
StorageDead(_8); // scope 2 at $DIR/generator-storage-dead-unwind.rs:26:15: 26:16
9393
StorageDead(_7); // scope 2 at $DIR/generator-storage-dead-unwind.rs:26:16: 26:17
94-
goto -> bb10; // scope 2 at $DIR/generator-storage-dead-unwind.rs:1:1: 1:1
94+
goto -> bb10; // scope 2 at no-location
9595
}
9696

9797
bb10 (cleanup): {

src/test/mir-opt/loop_test.main.SimplifyCfg-promote-consts.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main() -> () {
4343
_6 = const 1_i32; // scope 0 at $DIR/loop_test.rs:14:17: 14:18
4444
FakeRead(ForLet(None), _6); // scope 0 at $DIR/loop_test.rs:14:13: 14:14
4545
StorageDead(_6); // scope 0 at $DIR/loop_test.rs:16:5: 16:6
46-
goto -> bb3; // scope 0 at $DIR/loop_test.rs:1:1: 1:1
46+
goto -> bb3; // scope 0 at no-location
4747
}
4848

4949
bb5 (cleanup): {

src/test/mir-opt/match_arm_scopes.complicated_match.SimplifyCfg-initial.after-ElaborateDrops.after.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
_0 = const 3_i32; // scope 0 at $DIR/match-arm-scopes.rs:15:59: 15:60
9595
StorageDead(_10); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
9696
StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
97-
- goto -> bb23; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1
98-
+ goto -> bb20; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1
97+
- goto -> bb23; // scope 0 at no-location
98+
+ goto -> bb20; // scope 0 at no-location
9999
}
100100

101101
- bb10: {
@@ -150,8 +150,8 @@
150150
_0 = const 3_i32; // scope 0 at $DIR/match-arm-scopes.rs:15:59: 15:60
151151
StorageDead(_13); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
152152
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
153-
- goto -> bb23; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1
154-
+ goto -> bb20; // scope 0 at $DIR/match-arm-scopes.rs:1:1: 1:1
153+
- goto -> bb23; // scope 0 at no-location
154+
+ goto -> bb20; // scope 0 at no-location
155155
}
156156

157157
- bb15: {

src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn while_loop(_1: bool) -> () {
4040

4141
bb4: {
4242
StorageDead(_4); // scope 0 at $DIR/while-storage.rs:13:9: 13:10
43-
goto -> bb6; // scope 0 at $DIR/while-storage.rs:1:1: 1:1
43+
goto -> bb6; // scope 0 at no-location
4444
}
4545

4646
bb5: {

src/test/ui/borrowck/issue-85765.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let mut test = Vec::new();
3+
let rofl: &Vec<Vec<i32>> = &mut test;
4+
//~^ HELP consider changing this to be a mutable reference
5+
rofl.push(Vec::new());
6+
//~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
7+
//~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
8+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
2+
--> $DIR/issue-85765.rs:5:5
3+
|
4+
LL | let rofl: &Vec<Vec<i32>> = &mut test;
5+
| ---- help: consider changing this to be a mutable reference: `&mut Vec<Vec<i32>>`
6+
LL |
7+
LL | rofl.push(Vec::new());
8+
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0596`.

src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error: lifetime parameters must be declared prior to const parameters
22
--> $DIR/intermixed-lifetime.rs:7:28
33
|
44
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
5-
| -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
5+
| -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`
66

77
error: lifetime parameters must be declared prior to type parameters
88
--> $DIR/intermixed-lifetime.rs:10:37
99
|
1010
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
11-
| --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
11+
| --------------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/const-generics/defaults/mismatch.full.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ LL | let e: Example::<13> = ();
55
| ------------- ^^ expected struct `Example`, found `()`
66
| |
77
| expected due to this
8+
|
9+
= note: expected struct `Example`
10+
found unit type `()`
811

912
error[E0308]: mismatched types
10-
--> $DIR/mismatch.rs:14:34
13+
--> $DIR/mismatch.rs:15:34
1114
|
1215
LL | let e: Example2::<u32, 13> = ();
1316
| ------------------- ^^ expected struct `Example2`, found `()`
@@ -18,7 +21,7 @@ LL | let e: Example2::<u32, 13> = ();
1821
found unit type `()`
1922

2023
error[E0308]: mismatched types
21-
--> $DIR/mismatch.rs:16:34
24+
--> $DIR/mismatch.rs:18:34
2225
|
2326
LL | let e: Example3::<13, u32> = ();
2427
| ------------------- ^^ expected struct `Example3`, found `()`
@@ -29,7 +32,7 @@ LL | let e: Example3::<13, u32> = ();
2932
found unit type `()`
3033

3134
error[E0308]: mismatched types
32-
--> $DIR/mismatch.rs:18:28
35+
--> $DIR/mismatch.rs:21:28
3336
|
3437
LL | let e: Example3::<7> = ();
3538
| ------------- ^^ expected struct `Example3`, found `()`
@@ -40,12 +43,15 @@ LL | let e: Example3::<7> = ();
4043
found unit type `()`
4144

4245
error[E0308]: mismatched types
43-
--> $DIR/mismatch.rs:22:28
46+
--> $DIR/mismatch.rs:24:28
4447
|
4548
LL | let e: Example4::<7> = ();
4649
| ------------- ^^ expected struct `Example4`, found `()`
4750
| |
4851
| expected due to this
52+
|
53+
= note: expected struct `Example4<7_usize>`
54+
found unit type `()`
4955

5056
error: aborting due to 5 previous errors
5157

src/test/ui/const-generics/defaults/mismatch.min.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ LL | let e: Example::<13> = ();
55
| ------------- ^^ expected struct `Example`, found `()`
66
| |
77
| expected due to this
8+
|
9+
= note: expected struct `Example`
10+
found unit type `()`
811

912
error[E0308]: mismatched types
10-
--> $DIR/mismatch.rs:14:34
13+
--> $DIR/mismatch.rs:15:34
1114
|
1215
LL | let e: Example2::<u32, 13> = ();
1316
| ------------------- ^^ expected struct `Example2`, found `()`
@@ -18,7 +21,7 @@ LL | let e: Example2::<u32, 13> = ();
1821
found unit type `()`
1922

2023
error[E0308]: mismatched types
21-
--> $DIR/mismatch.rs:16:34
24+
--> $DIR/mismatch.rs:18:34
2225
|
2326
LL | let e: Example3::<13, u32> = ();
2427
| ------------------- ^^ expected struct `Example3`, found `()`
@@ -29,7 +32,7 @@ LL | let e: Example3::<13, u32> = ();
2932
found unit type `()`
3033

3134
error[E0308]: mismatched types
32-
--> $DIR/mismatch.rs:18:28
35+
--> $DIR/mismatch.rs:21:28
3336
|
3437
LL | let e: Example3::<7> = ();
3538
| ------------- ^^ expected struct `Example3`, found `()`
@@ -40,12 +43,15 @@ LL | let e: Example3::<7> = ();
4043
found unit type `()`
4144

4245
error[E0308]: mismatched types
43-
--> $DIR/mismatch.rs:22:28
46+
--> $DIR/mismatch.rs:24:28
4447
|
4548
LL | let e: Example4::<7> = ();
4649
| ------------- ^^ expected struct `Example4`, found `()`
4750
| |
4851
| expected due to this
52+
|
53+
= note: expected struct `Example4<7_usize>`
54+
found unit type `()`
4955

5056
error: aborting due to 5 previous errors
5157

src/test/ui/const-generics/defaults/mismatch.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ pub struct Example4<const N: usize=13, const M: usize=4>;
1111
fn main() {
1212
let e: Example::<13> = ();
1313
//~^ Error: mismatched types
14+
//~| expected struct `Example`
1415
let e: Example2::<u32, 13> = ();
1516
//~^ Error: mismatched types
17+
//~| expected struct `Example2`
1618
let e: Example3::<13, u32> = ();
1719
//~^ Error: mismatched types
20+
//~| expected struct `Example3`
1821
let e: Example3::<7> = ();
1922
//~^ Error: mismatched types
20-
// FIXME(const_generics_defaults): There should be a note for the error below, but it is
21-
// missing.
23+
//~| expected struct `Example3<7_usize>`
2224
let e: Example4::<7> = ();
2325
//~^ Error: mismatched types
26+
//~| expected struct `Example4<7_usize>`
2427
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(const_generics_defaults)]
2+
struct Foo<const M: usize = 10, 'a>(&'a u32);
3+
//~^ Error lifetime parameters must be declared prior to const parameters
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: lifetime parameters must be declared prior to const parameters
2+
--> $DIR/param-order-err-pretty-prints-default.rs:2:33
3+
|
4+
LL | struct Foo<const M: usize = 10, 'a>(&'a u32);
5+
| ----------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const M: usize = 10>`
6+
7+
error: aborting due to previous error
8+

src/test/ui/const-generics/min_const_generics/transmute-const-param-static-reference.rs

-10
This file was deleted.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `&'static ()` is forbidden as the type of a const generic parameter
2-
--> $DIR/transmute-const-param-static-reference.rs:1:23
2+
--> $DIR/transmute-const-param-static-reference.rs:7:23
33
|
44
LL | struct Const<const P: &'static ()>;
55
| ^^^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// revisions: full min
2+
//[full] check-pass
3+
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(full, allow(incomplete_features))]
6+
7+
struct Const<const P: &'static ()>;
8+
//[min]~^ ERROR `&'static ()` is forbidden as the type of a const generic parameter
9+
10+
fn main() {
11+
const A: &'static () = unsafe {
12+
std::mem::transmute(10 as *const ())
13+
};
14+
15+
let _ = Const::<{A}>;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This test contains code with incorrect vtables in a const context:
2+
// - from issue 86132: a trait object with invalid alignment caused an ICE in const eval, and now
3+
// triggers an error
4+
// - a similar test that triggers a previously-untested const UB error: emitted close to the above
5+
// error, it checks the correctness of the size
6+
7+
trait Trait {}
8+
9+
const INVALID_VTABLE_ALIGNMENT: &dyn Trait =
10+
unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
11+
//~^ ERROR any use of this value will cause an error
12+
//~| WARNING this was previously accepted by the compiler
13+
//~| invalid vtable: alignment `1000` is not a power of 2
14+
15+
const INVALID_VTABLE_SIZE: &dyn Trait =
16+
unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
17+
//~^ ERROR any use of this value will cause an error
18+
//~| WARNING this was previously accepted by the compiler
19+
//~| invalid vtable: size is bigger than largest supported object
20+
21+
fn main() {}

0 commit comments

Comments
 (0)