Skip to content

Commit

Permalink
Auto merge of rust-lang#86226 - JohnTitor:rollup-5ubdolf, r=JohnTitor
Browse files Browse the repository at this point in the history
Rollup of 7 pull requests

Successful merges:

 - rust-lang#85800 (Fix some diagnostic issues with const_generics_defaults feature gate)
 - rust-lang#85823 (Do not suggest ampmut if rhs is already mutable)
 - rust-lang#86153 (Print dummy spans as `no-location`)
 - rust-lang#86174 (Detect incorrect vtable alignment during const eval)
 - rust-lang#86189 (Make `relate_type_and_mut` public)
 - rust-lang#86205 (Run full const-generics test for issue-72293)
 - rust-lang#86217 (Remove "generic type" in boxed.rs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 12, 2021
2 parents 0a8629b + e13b53e commit 3198c52
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 41 deletions.
9 changes: 6 additions & 3 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.non_erasable_generics().next().is_none(),
Ref(_, ty, _) => ty.is_simple_text(),
_ => self.is_simple_ty(),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_mir/src/interpret/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ 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!(
"invalid vtable: \
size is bigger than largest supported object"
);
}
Ok((Size::from_bytes(size), Align::from_bytes(align).unwrap()))
Ok((Size::from_bytes(size), align))
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ impl<T: ?Sized + Hasher, A: Allocator> Hasher for Box<T, A> {
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "from_for_ptrs", since = "1.6.0")]
impl<T> From<T> for Box<T> {
/// Converts a generic type `T` into a `Box<T>`
/// Converts a `T` into a `Box<T>`
///
/// The conversion allocates on the heap and moves `t`
/// from the stack into it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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): {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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): {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/borrowck/issue-85765.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let mut test = Vec::new();
let rofl: &Vec<Vec<i32>> = &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
}
12 changes: 12 additions & 0 deletions src/test/ui/borrowck/issue-85765.stderr
Original file line number Diff line number Diff line change
@@ -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<Vec<i32>> = &mut test;
| ---- help: consider changing this to be a mutable reference: `&mut Vec<Vec<i32>>`
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`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ error: lifetime parameters must be declared prior to const parameters
--> $DIR/intermixed-lifetime.rs:7:28
|
LL | struct Foo<const N: usize, 'a, T = u32>(&'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<const N: usize, T = u32, 'a>(&'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

14 changes: 10 additions & 4 deletions src/test/ui/const-generics/defaults/mismatch.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ 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
--> $DIR/mismatch.rs:15:34
|
LL | let e: Example2::<u32, 13> = ();
| ------------------- ^^ expected struct `Example2`, found `()`
Expand All @@ -18,7 +21,7 @@ LL | let e: Example2::<u32, 13> = ();
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 `()`
Expand All @@ -29,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 `()`
Expand All @@ -40,12 +43,15 @@ LL | let e: Example3::<7> = ();
found unit type `()`

error[E0308]: mismatched types
--> $DIR/mismatch.rs:22:28
--> $DIR/mismatch.rs:24: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

Expand Down
14 changes: 10 additions & 4 deletions src/test/ui/const-generics/defaults/mismatch.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ 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
--> $DIR/mismatch.rs:15:34
|
LL | let e: Example2::<u32, 13> = ();
| ------------------- ^^ expected struct `Example2`, found `()`
Expand All @@ -18,7 +21,7 @@ LL | let e: Example2::<u32, 13> = ();
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 `()`
Expand All @@ -29,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 `()`
Expand All @@ -40,12 +43,15 @@ LL | let e: Example3::<7> = ();
found unit type `()`

error[E0308]: mismatched types
--> $DIR/mismatch.rs:22:28
--> $DIR/mismatch.rs:24: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

Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/const-generics/defaults/mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ pub struct Example4<const N: usize=13, const M: usize=4>;
fn main() {
let e: Example::<13> = ();
//~^ Error: mismatched types
//~| expected struct `Example`
let e: Example2::<u32, 13> = ();
//~^ Error: mismatched types
//~| expected struct `Example2`
let e: Example3::<13, u32> = ();
//~^ Error: mismatched types
//~| expected struct `Example3`
let e: Example3::<7> = ();
//~^ Error: mismatched types
// FIXME(const_generics_defaults): There should be a note for the error below, but it is
// missing.
//~| expected struct `Example3<7_usize>`
let e: Example4::<7> = ();
//~^ Error: mismatched types
//~| expected struct `Example4<7_usize>`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![feature(const_generics_defaults)]
struct Foo<const M: usize = 10, 'a>(&'a u32);
//~^ Error lifetime parameters must be declared prior to const parameters

fn main() {}
Original file line number Diff line number Diff line change
@@ -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<const M: usize = 10, 'a>(&'a u32);
| ----------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const M: usize = 10>`

error: aborting due to previous error

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<const P: &'static ()>;
| ^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// revisions: full min
//[full] check-pass

#![cfg_attr(full, feature(const_generics))]
#![cfg_attr(full, allow(incomplete_features))]

struct Const<const P: &'static ()>;
//[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}>;
}
21 changes: 21 additions & 0 deletions src/test/ui/consts/const-eval/ub-incorrect-vtable.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading

0 comments on commit 3198c52

Please sign in to comment.