Skip to content

Commit

Permalink
Normalize raw pointers (#13833)
Browse files Browse the repository at this point in the history
Consider raw pointers fields as normalizable.

This is only used to determine the layout, and the layout of a raw
pointer is known, whatever the designated type is. As a side effect,
this makes the layout of `Box<T>` known, even inside `T`, and enables
recursive types.

Would there be a drawback in doing this?

I have included the new tests in a separate commit, so that the effect
of the change can be seen in the second commit. It can also be seen on
the lintcheck diff result.

Close #9798.
  • Loading branch information
llogiq authored Dec 16, 2024
2 parents 968669b + cbfa74b commit 56627c8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions clippy_utils/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ fn is_normalizable_helper<'tcx>(
.iter()
.all(|field| is_normalizable_helper(cx, param_env, field.ty(cx.tcx, args), cache))
}),
ty::RawPtr(..) => true,
_ => ty.walk().all(|generic_arg| match generic_arg.unpack() {
GenericArgKind::Type(inner_ty) if inner_ty != ty => {
is_normalizable_helper(cx, param_env, inner_ty, cache)
Expand Down
34 changes: 33 additions & 1 deletion tests/ui/large_enum_variant.64bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,37 @@ help: consider boxing the large fields to reduce the total size of the enum
LL | Error(Box<PossiblyLargeEnumWithConst<256>>),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 16 previous errors
error: large size difference between variants
--> tests/ui/large_enum_variant.rs:158:1
|
LL | / enum WithRecursion {
LL | | Large([u64; 64]),
| | ---------------- the largest variant contains at least 512 bytes
LL | | Recursive(Box<WithRecursion>),
| | ----------------------------- the second-largest variant contains at least 8 bytes
LL | | }
| |_^ the entire enum is at least 520 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
|
LL | Large(Box<[u64; 64]>),
| ~~~~~~~~~~~~~~

error: large size difference between variants
--> tests/ui/large_enum_variant.rs:168:1
|
LL | / enum LargeEnumWithGenericsAndRecursive {
LL | | Ok(),
| | ---- the second-largest variant carries no data at all
LL | | Error(WithRecursionAndGenerics<u64>),
| | ------------------------------------ the largest variant contains at least 520 bytes
LL | | }
| |_^ the entire enum is at least 520 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
|
LL | Error(Box<WithRecursionAndGenerics<u64>>),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 18 previous errors

15 changes: 15 additions & 0 deletions tests/ui/large_enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ enum LargeEnumOfConst {
Error(PossiblyLargeEnumWithConst<256>),
}

enum WithRecursion {
Large([u64; 64]),
Recursive(Box<WithRecursion>),
}

enum WithRecursionAndGenerics<T> {
Large([T; 64]),
Recursive(Box<WithRecursionAndGenerics<T>>),
}

enum LargeEnumWithGenericsAndRecursive {
Ok(),
Error(WithRecursionAndGenerics<u64>),
}

fn main() {
external!(
enum LargeEnumInMacro {
Expand Down

0 comments on commit 56627c8

Please sign in to comment.