Skip to content
forked from rust-lang/rust
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4ffe0cc

Browse files
authoredMar 22, 2024
Unrolled build for rust-lang#122820
Rollup merge of rust-lang#122820 - oli-obk:no_ord_def_id, r=estebank Stop using `<DefId as Ord>` in various diagnostic situations work towards rust-lang#90317 Reverts part of rust-lang#106281, as it sorts constants and that's problematic since it can contain `ParamConst`, which contains `DefId`s
2 parents eff958c + d8470bb commit 4ffe0cc

File tree

10 files changed

+197
-161
lines changed

10 files changed

+197
-161
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,7 @@ name = "rustc_middle"
42364236
version = "0.0.0"
42374237
dependencies = [
42384238
"bitflags 2.4.2",
4239+
"derivative",
42394240
"either",
42404241
"field-offset",
42414242
"gsgdt",

‎compiler/rustc_middle/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
# tidy-alphabetical-start
88
bitflags = "2.4.1"
9+
derivative = "2.2.0"
910
either = "1.5.0"
1011
field-offset = "0.3.5"
1112
gsgdt = "0.1.2"

‎compiler/rustc_middle/src/mir/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'tcx> Const<'tcx> {
456456
}
457457

458458
/// An unevaluated (potentially generic) constant used in MIR.
459-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
459+
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable)]
460460
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable, Lift)]
461461
pub struct UnevaluatedConst<'tcx> {
462462
pub def: DefId,

‎compiler/rustc_middle/src/mir/query.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,15 @@ rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 16);
284284
/// order of the category, thereby influencing diagnostic output.
285285
///
286286
/// See also `rustc_const_eval::borrow_check::constraints`.
287-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
287+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
288288
#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
289+
#[derive(derivative::Derivative)]
290+
#[derivative(
291+
PartialOrd,
292+
Ord,
293+
PartialOrd = "feature_allow_slow_enum",
294+
Ord = "feature_allow_slow_enum"
295+
)]
289296
pub enum ConstraintCategory<'tcx> {
290297
Return(ReturnConstraint),
291298
Yield,
@@ -295,6 +302,7 @@ pub enum ConstraintCategory<'tcx> {
295302
Cast {
296303
/// Whether this is an unsizing cast and if yes, this contains the target type.
297304
/// Region variables are erased to ReErased.
305+
#[derivative(PartialOrd = "ignore", Ord = "ignore")]
298306
unsize_to: Option<Ty<'tcx>>,
299307
},
300308

@@ -304,7 +312,7 @@ pub enum ConstraintCategory<'tcx> {
304312
ClosureBounds,
305313

306314
/// Contains the function type if available.
307-
CallArgument(Option<Ty<'tcx>>),
315+
CallArgument(#[derivative(PartialOrd = "ignore", Ord = "ignore")] Option<Ty<'tcx>>),
308316
CopyBound,
309317
SizedBound,
310318
Assignment,

‎compiler/rustc_middle/src/ty/layout.rs

+3-64
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::UnsupportedFnAbi;
22
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
33
use crate::query::TyCtxtAt;
44
use crate::ty::normalize_erasing_regions::NormalizationError;
5-
use crate::ty::{self, ConstKind, Ty, TyCtxt, TypeVisitableExt};
5+
use crate::ty::{self, Ty, TyCtxt, TypeVisitableExt};
66
use rustc_error_messages::DiagMessage;
77
use rustc_errors::{
88
Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, IntoDiagArg, Level,
@@ -356,21 +356,10 @@ impl<'tcx> SizeSkeleton<'tcx> {
356356
.ok_or_else(|| &*tcx.arena.alloc(LayoutError::SizeOverflow(ty)))?;
357357
return Ok(SizeSkeleton::Known(Size::from_bytes(size)));
358358
}
359-
let len = tcx.expand_abstract_consts(len);
360-
let prev = ty::Const::from_target_usize(tcx, s.bytes());
361-
let Some(gen_size) = mul_sorted_consts(tcx, param_env, len, prev) else {
362-
return Err(tcx.arena.alloc(LayoutError::SizeOverflow(ty)));
363-
};
364-
Ok(SizeSkeleton::Generic(gen_size))
359+
Err(tcx.arena.alloc(LayoutError::Unknown(ty)))
365360
}
366361
SizeSkeleton::Pointer { .. } => Err(err),
367-
SizeSkeleton::Generic(g) => {
368-
let len = tcx.expand_abstract_consts(len);
369-
let Some(gen_size) = mul_sorted_consts(tcx, param_env, len, g) else {
370-
return Err(tcx.arena.alloc(LayoutError::SizeOverflow(ty)));
371-
};
372-
Ok(SizeSkeleton::Generic(gen_size))
373-
}
362+
SizeSkeleton::Generic(_) => Err(tcx.arena.alloc(LayoutError::Unknown(ty))),
374363
}
375364
}
376365

@@ -468,56 +457,6 @@ impl<'tcx> SizeSkeleton<'tcx> {
468457
}
469458
}
470459

471-
/// When creating the layout for types with abstract consts in their size (i.e. [usize; 4 * N]),
472-
/// to ensure that they have a canonical order and can be compared directly we combine all
473-
/// constants, and sort the other terms. This allows comparison of expressions of sizes,
474-
/// allowing for things like transmuting between types that depend on generic consts.
475-
/// This returns `None` if multiplication of constants overflows.
476-
fn mul_sorted_consts<'tcx>(
477-
tcx: TyCtxt<'tcx>,
478-
param_env: ty::ParamEnv<'tcx>,
479-
a: ty::Const<'tcx>,
480-
b: ty::Const<'tcx>,
481-
) -> Option<ty::Const<'tcx>> {
482-
use crate::mir::BinOp::Mul;
483-
484-
let mut work = vec![a, b];
485-
let mut done = vec![];
486-
while let Some(n) = work.pop() {
487-
if let ConstKind::Expr(ty::Expr::Binop(Mul, l, r)) = n.kind() {
488-
work.push(l);
489-
work.push(r)
490-
} else {
491-
done.push(n);
492-
}
493-
}
494-
let mut k = 1;
495-
let mut overflow = false;
496-
done.retain(|c| {
497-
let Some(c) = c.try_eval_target_usize(tcx, param_env) else {
498-
return true;
499-
};
500-
let Some(next) = c.checked_mul(k) else {
501-
overflow = true;
502-
return false;
503-
};
504-
k = next;
505-
false
506-
});
507-
if overflow {
508-
return None;
509-
}
510-
if k != 1 {
511-
done.push(ty::Const::from_target_usize(tcx, k));
512-
} else if k == 0 {
513-
return Some(ty::Const::from_target_usize(tcx, 0));
514-
}
515-
done.sort_unstable();
516-
517-
// create a single tree from the buffer
518-
done.into_iter().reduce(|acc, n| ty::Const::new_expr(tcx, ty::Expr::Binop(Mul, n, acc), n.ty()))
519-
}
520-
521460
pub trait HasTyCtxt<'tcx>: HasDataLayout {
522461
fn tcx(&self) -> TyCtxt<'tcx>;
523462
}

‎tests/ui/const-generics/transmute-fail.rs

+75
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,79 @@ fn overflow(v: [[[u32; 8888888]; 9999999]; 777777777]) -> [[[u32; 9999999]; 7777
3232
}
3333
}
3434

35+
fn transpose<const W: usize, const H: usize>(v: [[u32;H]; W]) -> [[u32; W]; H] {
36+
unsafe {
37+
std::mem::transmute(v)
38+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
39+
}
40+
}
41+
42+
fn ident<const W: usize, const H: usize>(v: [[u32; H]; W]) -> [[u32; H]; W] {
43+
unsafe {
44+
std::mem::transmute(v)
45+
}
46+
}
47+
48+
fn flatten<const W: usize, const H: usize>(v: [[u32; H]; W]) -> [u32; W * H] {
49+
unsafe {
50+
std::mem::transmute(v)
51+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
52+
}
53+
}
54+
55+
fn coagulate<const W: usize, const H: usize>(v: [u32; H*W]) -> [[u32; W];H] {
56+
unsafe {
57+
std::mem::transmute(v)
58+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
59+
}
60+
}
61+
62+
fn flatten_3d<const W: usize, const H: usize, const D: usize>(
63+
v: [[[u32; D]; H]; W]
64+
) -> [u32; D * W * H] {
65+
unsafe {
66+
std::mem::transmute(v)
67+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
68+
}
69+
}
70+
71+
fn flatten_somewhat<const W: usize, const H: usize, const D: usize>(
72+
v: [[[u32; D]; H]; W]
73+
) -> [[u32; D * W]; H] {
74+
unsafe {
75+
std::mem::transmute(v)
76+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
77+
}
78+
}
79+
80+
fn known_size<const L: usize>(v: [u16; L]) -> [u8; L * 2] {
81+
unsafe {
82+
std::mem::transmute(v)
83+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
84+
}
85+
}
86+
87+
fn condense_bytes<const L: usize>(v: [u8; L * 2]) -> [u16; L] {
88+
unsafe {
89+
std::mem::transmute(v)
90+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
91+
}
92+
}
93+
94+
fn singleton_each<const L: usize>(v: [u8; L]) -> [[u8;1]; L] {
95+
unsafe {
96+
std::mem::transmute(v)
97+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
98+
}
99+
}
100+
101+
fn transpose_with_const<const W: usize, const H: usize>(
102+
v: [[u32; 2 * H]; W + W]
103+
) -> [[u32; W + W]; 2 * H] {
104+
unsafe {
105+
std::mem::transmute(v)
106+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
107+
}
108+
}
109+
35110
fn main() {}

‎tests/ui/const-generics/transmute-fail.stderr

+86-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
44
LL | std::mem::transmute(v)
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: `[[u32; H+1]; W]` (generic size (H + 1) * 4 * W)
8-
= note: target type: `[[u32; W+1]; H]` (generic size (W + 1) * 4 * H)
7+
= note: source type: `[[u32; H+1]; W]` (size can vary because of [u32; H+1])
8+
= note: target type: `[[u32; W+1]; H]` (size can vary because of [u32; W+1])
99

1010
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
1111
--> $DIR/transmute-fail.rs:16:5
@@ -22,8 +22,8 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
2222
LL | std::mem::transmute(v)
2323
| ^^^^^^^^^^^^^^^^^^^
2424
|
25-
= note: source type: `[[u32; H]; W]` (generic size 4 * H * W)
26-
= note: target type: `[u32; W * H * H]` (generic size 4 * H * H * W)
25+
= note: source type: `[[u32; H]; W]` (size can vary because of [u32; H])
26+
= note: target type: `[u32; W * H * H]` (this type does not have a fixed size)
2727

2828
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2929
--> $DIR/transmute-fail.rs:30:5
@@ -34,6 +34,87 @@ LL | std::mem::transmute(v)
3434
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[u32; 8888888]; 9999999]` are too big for the current architecture)
3535
= note: target type: `[[[u32; 9999999]; 777777777]; 8888888]` (values of the type `[[u32; 9999999]; 777777777]` are too big for the current architecture)
3636

37+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
38+
--> $DIR/transmute-fail.rs:37:5
39+
|
40+
LL | std::mem::transmute(v)
41+
| ^^^^^^^^^^^^^^^^^^^
42+
|
43+
= note: source type: `[[u32; H]; W]` (size can vary because of [u32; H])
44+
= note: target type: `[[u32; W]; H]` (size can vary because of [u32; W])
45+
46+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
47+
--> $DIR/transmute-fail.rs:50:5
48+
|
49+
LL | std::mem::transmute(v)
50+
| ^^^^^^^^^^^^^^^^^^^
51+
|
52+
= note: source type: `[[u32; H]; W]` (size can vary because of [u32; H])
53+
= note: target type: `[u32; W * H]` (this type does not have a fixed size)
54+
55+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
56+
--> $DIR/transmute-fail.rs:57:5
57+
|
58+
LL | std::mem::transmute(v)
59+
| ^^^^^^^^^^^^^^^^^^^
60+
|
61+
= note: source type: `[u32; H*W]` (this type does not have a fixed size)
62+
= note: target type: `[[u32; W]; H]` (size can vary because of [u32; W])
63+
64+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
65+
--> $DIR/transmute-fail.rs:66:5
66+
|
67+
LL | std::mem::transmute(v)
68+
| ^^^^^^^^^^^^^^^^^^^
69+
|
70+
= note: source type: `[[[u32; D]; H]; W]` (size can vary because of [u32; D])
71+
= note: target type: `[u32; D * W * H]` (this type does not have a fixed size)
72+
73+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
74+
--> $DIR/transmute-fail.rs:75:5
75+
|
76+
LL | std::mem::transmute(v)
77+
| ^^^^^^^^^^^^^^^^^^^
78+
|
79+
= note: source type: `[[[u32; D]; H]; W]` (size can vary because of [u32; D])
80+
= note: target type: `[[u32; D * W]; H]` (size can vary because of [u32; D * W])
81+
82+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
83+
--> $DIR/transmute-fail.rs:82:5
84+
|
85+
LL | std::mem::transmute(v)
86+
| ^^^^^^^^^^^^^^^^^^^
87+
|
88+
= note: source type: `[u16; L]` (this type does not have a fixed size)
89+
= note: target type: `[u8; L * 2]` (this type does not have a fixed size)
90+
91+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
92+
--> $DIR/transmute-fail.rs:89:5
93+
|
94+
LL | std::mem::transmute(v)
95+
| ^^^^^^^^^^^^^^^^^^^
96+
|
97+
= note: source type: `[u8; L * 2]` (this type does not have a fixed size)
98+
= note: target type: `[u16; L]` (this type does not have a fixed size)
99+
100+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
101+
--> $DIR/transmute-fail.rs:96:5
102+
|
103+
LL | std::mem::transmute(v)
104+
| ^^^^^^^^^^^^^^^^^^^
105+
|
106+
= note: source type: `[u8; L]` (this type does not have a fixed size)
107+
= note: target type: `[[u8; 1]; L]` (this type does not have a fixed size)
108+
109+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
110+
--> $DIR/transmute-fail.rs:105:5
111+
|
112+
LL | std::mem::transmute(v)
113+
| ^^^^^^^^^^^^^^^^^^^
114+
|
115+
= note: source type: `[[u32; 2 * H]; W + W]` (size can vary because of [u32; 2 * H])
116+
= note: target type: `[[u32; W + W]; 2 * H]` (size can vary because of [u32; W + W])
117+
37118
error[E0308]: mismatched types
38119
--> $DIR/transmute-fail.rs:12:53
39120
|
@@ -46,7 +127,7 @@ error[E0308]: mismatched types
46127
LL | fn bar<const W: bool, const H: usize>(v: [[u32; H]; W]) -> [[u32; W]; H] {
47128
| ^ expected `usize`, found `bool`
48129

49-
error: aborting due to 6 previous errors
130+
error: aborting due to 15 previous errors
50131

51132
Some errors have detailed explanations: E0308, E0512.
52133
For more information about an error, try `rustc --explain E0308`.
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.