Commit Graph

161 Commits

Author SHA1 Message Date
Nicholas Nethercote
d602394827 Change message type in bug functions.
From `impl Into<DiagnosticMessage>` to `impl Into<Cow<'static, str>>`.

Because these functions don't produce user-facing output and we don't
want their strings to be translated.
2024-03-05 17:11:42 +11:00
Erik Desjardins
f574c65147 simplify common prim computation 2024-02-28 17:55:04 -05:00
Trevor Gross
e3f63d9375 Add f16 and f128 to rustc_type_ir::FloatTy and rustc_abi::Primitive
Make changes necessary to support these types in the compiler.
2024-02-28 12:58:32 -05:00
Erik Desjardins
4dabbcb23b allow using scalarpair with a common prim of ptr/ptr-sized-int 2024-02-27 00:09:12 -05:00
Ralf Jung
297abc35b3 fix some references to no-longer-existing ReprOptions.layout_seed 2024-02-26 10:40:48 +01:00
Nadrieril
e12101c4db Fix rustc_abi build on stable 2024-01-16 21:15:31 +01:00
bjorn3
6ed37bdc42 Avoid specialization for the Span Encodable and Decodable impls 2023-12-31 20:42:17 +00:00
Nilstrieb
ffafcd8819 Update to bitflags 2 in the compiler
This involves lots of breaking changes. There are two big changes that
force changes. The first is that the bitflag types now don't
automatically implement normal derive traits, so we need to derive them
manually.

Additionally, bitflags now have a hidden inner type by default, which
breaks our custom derives. The bitflags docs recommend using the impl
form in these cases, which I did.
2023-12-30 18:17:28 +01:00
Jubilee Young
8facfb23c6 Extract fn layout_of_struct 2023-12-16 02:36:35 -08:00
Jubilee Young
b525f76bb5 Extract fn layout_of_enum 2023-12-16 02:31:57 -08:00
Jubilee Young
f116bc6e27 Manually inline scalar_unit 2023-12-16 01:43:40 -08:00
Jubilee Young
6959818ec2 Hoist fn absent 2023-12-16 01:41:56 -08:00
Jubilee Young
0e9d400587 Annotate panic! reasons during enum layout
Add some reasons to the panics, and use more exhaustive matches.
2023-12-15 02:21:31 -08:00
Nicholas Nethercote
cb91235131 Rename LayoutCalculator::delay_bug as LayoutCalculator::delayed_bug.
To match with the previous commits.
2023-12-02 09:01:34 +11:00
Nilstrieb
21a870515b Fix clippy::needless_borrow in the compiler
`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`.

Then I had to remove a few unnecessary parens and muts that were exposed
now.
2023-11-21 20:13:40 +01:00
Mark Rousskov
db3e2bacb6 Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
hkalbasi
c8a25eddfe Make the randomize feature of rustc_abi additive 2023-11-05 21:51:47 +03:30
Nicholas Nethercote
8ff624a9f2 Clean up rustc_*/Cargo.toml.
- Sort dependencies and features sections.
- Add `tidy` markers to the sorted sections so they stay sorted.
- Remove empty `[lib`] sections.
- Remove "See more keys..." comments.

Excluded files:
- rustc_codegen_{cranelift,gcc}, because they're external.
- rustc_lexer, because it has external use.
- stable_mir, because it has external use.
2023-10-30 08:46:02 +11:00
Ralf Jung
2ef5897a89 fix failure to detect a too-big-type after adding padding 2023-10-27 18:07:53 +02:00
Oli Scherer
e96ce20b34 s/generator/coroutine/ 2023-10-20 21:14:01 +00:00
Michael Howell
2ff2624722 docs: add Rust logo to more compiler crates
c6e6ecb1af added it to some of the
compiler's crates, but avoided adding it to all of them to reduce
bit-rot. This commit adds to more.
2023-10-16 15:38:08 -07:00
Ralf Jung
f3f9b795bd place evaluation: require the original pointer to be aligned if an access happens 2023-10-15 18:13:31 +02:00
Lukas Wirth
0aedec4849 Remove unnecessary features from rustc_abi 2023-10-04 11:43:57 +02:00
Lukas Wirth
2d73d30a3c Fix incorrect inlining of RangeInclusive::size_hint 2023-10-02 21:31:16 +02:00
Lukas Wirth
b25c84510d Add some docs regarding rustc_abi rust-analyzer compat changes 2023-10-02 21:31:16 +02:00
Lukas Wirth
67e5eb6cec Cfg out ReprOption::field_shuffle_seed 2023-10-02 21:31:16 +02:00
Lukas Wirth
e8a2673159 Add VariantIdx back 2023-10-02 21:31:16 +02:00
Lukas Wirth
f14b7c9443 Move FieldIdx and Layout to rustc_target 2023-10-02 21:31:16 +02:00
Lukas Wirth
b47ad3b744 Bring back generic FieldIdx 2023-10-02 21:31:11 +02:00
Lukas Wirth
3b99d73f5a Unglob rustc_abi imports 2023-10-02 21:29:19 +02:00
Nicholas Nethercote
17e3793eb1 Name some local variables more consistently.
By making every `alt_foo` exactly match a `foo`.
2023-10-02 09:12:47 +11:00
Nicholas Nethercote
2369adc5d8 Remove unnecessary pubs. 2023-10-01 21:57:09 +11:00
Nicholas Nethercote
ddb742225a Rename two parsing closures.
To match `parse_address_space` and `parse_bits` above.
2023-10-01 21:57:09 +11:00
Nicholas Nethercote
86ecfdd605 Minor comment and whitespace tweaks. 2023-10-01 21:57:06 +11:00
Ralf Jung
b0cf4c28ea turns out Layout has some more things to worry about -- move ABI comparison into helper function
like is_bool, and some special magic extra fields
2023-09-08 09:14:07 +02:00
Ralf Jung
c3e14edd8b accept some differences for rustc_abi(assert_eq), so that we can test more things to be compatible 2023-09-08 08:59:55 +02:00
Ralf Jung
bf91321e0f there seems to be no reason to treat ZST specially in these cases 2023-08-29 08:58:58 +02:00
Ralf Jung
0da9409e08 rustc_abi: audit uses of is_zst; fix a case of giving an enum insufficient alignment 2023-08-29 08:58:58 +02:00
Ralf Jung
0360b6740b add is_1zst helper method 2023-08-29 08:58:21 +02:00
Mark Rousskov
0a916062aa Bump cfg(bootstrap) 2023-08-23 20:05:14 -04:00
Nilstrieb
5830ca216d Add internal_features lint
It lints against features that are inteded to be internal to the
compiler and standard library. Implements MCP #596.

We allow `internal_features` in the standard library and compiler as those
use many features and this _is_ the standard library from the "internal to the compiler and
standard library" after all.

Marking some features as internal wasn't exactly the most scientific approach, I just marked some
mostly obvious features. While there is a categorization in the macro,
it's not very well upheld (should probably be fixed in another PR).

We always pass `-Ainternal_features` in the testsuite
About 400 UI tests and several other tests use internal features.
Instead of throwing the attribute on each one, just always allow them.
There's nothing wrong with testing internal features^^
2023-08-03 14:50:50 +02:00
Matthias Krüger
23815467a2 inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
Matthias Krüger
ba6982b8a0
Rollup merge of #114060 - davidtwco:issue-113279, r=wesleywiser
abi: unsized field in union - assert to delay bug

Fixes #113279.

> Unions cannot have unsized fields, and as such, layout computation for
unions asserts that each union field is sized (as this would normally
have halted compilation earlier).
>
> However, if a generator ends up with an unsized local - a circumstance
in which an error will always have been emitted earlier, for example, if
attempting to dereference a `&str` - then the generator transform will
produce a union with an unsized field.
>
> Since https://github.com/rust-lang/rust/pull/110107, later passes will be run, such as constant propagation,
and can attempt layout computation on the generator, which will result
in layout computation of `str` in the context of it being a field of a
union - and so the aforementioned assertion would cause an ICE.
>
> It didn't seem appropriate to try and detect this case in the MIR body
and skip this specific pass; tainting the MIR body or delaying a bug
from the generator transform (or elsewhere) wouldn't prevent this either
(as neither would prevent the later pass from running); and tainting when
the deref of `&str` is reported, if that's possible, would unnecessarily
prevent potential other errors from being reported later in compilation,
and is very tailored to this specific case of getting a unsized type in
a generator.
>
> Given that this circumstance can only happen when an error should have
already been reported, the correct fix appears to be just changing the
assert to a delayed bug. This will still assert if there is some
circumstance where this occurs and no error has been reported, but it
won't crash the compiler in this instance.

While debugging this, I noticed a translation ICE in a delayed bug, so I fixed that too:

> During borrowck, the `MultiSpan` from a buffered diagnostic is cloned and
used to emit a delayed bug indicating a diagnostic was buffered - when
the buffered diagnostic is translated, then the cloned `MultiSpan` may
contain labels which can only render with the diagnostic's arguments, but
the delayed bug being emitted won't have those arguments. Adds a function
which clones `MultiSpan` without also cloning the contained labels, and
use this function when creating the buffered diagnostic delayed bug.
2023-07-25 19:21:39 +02:00
David Wood
037b27430b
abi: unsized field in union - assert to delay bug
Unions cannot have unsized fields, and as such, layout computation for
unions asserts that each union field is sized (as this would normally
have halted compilation earlier).

However, if a generator ends up with an unsized local - a circumstance
in which an error will always have been emitted earlier, for example, if
attempting to dereference a `&str` - then the generator transform will
produce a union with an unsized field.

Since #110107, later passes will be run, such as constant propagation,
and can attempt layout computation on the generator, which will result
in layout computation of `str` in the context of it being a field of a
union - and so the aforementioned assertion would cause an ICE.

It didn't seem appropriate to try and detect this case in the MIR body
and skip this specific pass; tainting the MIR body or delaying a bug
from the generator transform (or elsewhere) wouldn't prevent this either
(as neither would prevent the later pass from running); and tainting when
the deref of `&str` is reported, if that's possible, would unnecessarily
prevent potential other errors from being reported later in compilation,
and is very tailored to this specific case of getting a unsized type in
a generator.

Given that this circumstance can only happen when an error should have
already been reported, the correct fix appears to be just changing the
assert to a delayed bug. This will still assert if there is some
circumstance where this occurs and no error has been reported, but it
won't crash the compiler in this instance.

Signed-off-by: David Wood <david@davidtw.co>
2023-07-25 15:50:39 +01:00
Ralf Jung
a2bcafa500 interpret: refactor projection code to work on a common trait, and use that for visitors 2023-07-25 14:30:58 +02:00
David Tolnay
5bbf0a8306
Revert "Auto merge of #113166 - moulins:ref-niches-initial, r=oli-obk"
This reverts commit 557359f925, reversing
changes made to 1e6c09a803.
2023-07-21 22:35:57 -07:00
Moulins
7f109086ee Track (partial) niche information in NaiveLayout
Still more complexity, but this allows computing exact `NaiveLayout`s
for null-optimized enums, and thus allows calls like
`transmute::<Option<&T>, &U>()` to work in generic contexts.
2023-07-21 14:23:23 +02:00
Moulins
39cfe70e4f CTFE: move target_{i, u}size_{min, max) to rustc_abi::TargetDataLayout 2023-07-21 03:31:47 +02:00
Moulins
4fb039ed6c recover null-ptr optimization by adding a special case to the niching logic 2023-07-21 03:31:46 +02:00
Moulins
76c49aead6 support non-null pointer niches in CTFE 2023-07-21 03:31:45 +02:00
Moulins
3c05276866 restrict the valid range of references if -Z reference-niches is set
Note that this doesn't actually work at all, as many places in rustc
assume that references only have the null niche.
2023-07-21 03:31:45 +02:00
Moulins
8b847ef734 add crate-local -Z reference_niches unstable flag (does nothing for now) 2023-07-21 03:31:45 +02:00
Erik Desjardins
2daacf5af9 i686-windows: make requested alignment > 4 special case apply transitively 2023-07-14 17:48:13 -04:00
Erik Desjardins
d1e764cb3b aarch64-linux: properly handle 128bit aligned aggregates 2023-07-10 19:19:40 -04:00
Erik Desjardins
7e933b4e26 repr(align) <= 4 should still be byval 2023-07-10 19:19:40 -04:00
Erik Desjardins
00b3eca0df move has_repr to layout, handle repr(transparent) properly 2023-07-10 19:19:39 -04:00
Lukas Markeffsky
478071ba9d clean up struct layout code 2023-07-06 13:04:13 +00:00
bors
6330daade9 Auto merge of #112062 - lukas-code:unsized-layout, r=wesleywiser
Make struct layout not depend on unsizeable tail

fixes (after backport) https://github.com/rust-lang/rust/issues/112048

Since unsizing `Ptr<Foo<T>>` -> `Ptr<Foo<U>` just copies the pointer and adds the metadata, the layout of `Foo` must not depend on niches in and alignment of the tail `T`.

Nominating for beta 1.71, because it will have this issue: `@rustbot` label beta-nominated
2023-06-13 22:34:59 +00:00
Andrew Xie
54d7b327e5 Removed stable/unstable sort arg from into_sorted_stable_ord, fixed a few misc issues, added collect to UnordItems 2023-06-08 00:38:50 -04:00
Deadbeef
4f83717cf7 Use translatable diagnostics in rustc_const_eval 2023-06-01 14:45:18 +00:00
The 8472
3003d05a8b disable some layout optimizations for unsizable structs 2023-05-29 20:16:18 +02:00
Lukas Markeffsky
381b778d27 Make struct layout not depend on unsizeable tail 2023-05-29 14:54:48 +02:00
Nicholas Nethercote
01e33a3600 Avoid &format("...") calls in error message code.
Error message all end up passing into a function as an `impl
Into<{D,Subd}iagnosticMessage>`. If an error message is creatd as
`&format("...")` that means we allocate a string (in the `format!`
call), then take a reference, and then clone (allocating again) the
reference to produce the `{D,Subd}iagnosticMessage`, which is silly.

This commit removes the leading `&` from a lot of these cases. This
means the original `String` is moved into the
`{D,Subd}iagnosticMessage`, avoiding the double allocations. This
requires changing some function argument types from `&str` to `String`
(when all arguments are `String`) or `impl
Into<{D,Subd}iagnosticMessage>` (when some arguments are `String` and
some are `&str`).
2023-05-16 17:59:56 +10:00
Luqman Aden
8e7714d3bb Reorder to keep duplicate checks in sync. 2023-05-05 16:30:32 -07:00
Luqman Aden
012f9a333b Review feedback 2023-05-05 16:14:36 -07:00
Luqman Aden
c63a204e23 Don't discard preferred alignment in scalar pair. 2023-05-05 16:11:08 -07:00
Luqman Aden
3b1e535f36 Factor out checks in layout check and add helper inherent_size. 2023-05-05 16:11:03 -07:00
Luqman Aden
4f4f22b11c Incorporate review feedback from 103926. 2023-05-05 16:04:59 -07:00
Luqman Aden
a3800535b1 Add helper methods inherent_align and to_union on Abi. 2023-05-05 16:00:19 -07:00
Oli Scherer
23d09aebc8 Do not use scalar layout if there are ZSTs with alignment > 1 2023-05-05 16:00:12 -07:00
The 8472
61fb5a91b7 layout-alignment-promotion logic should depend on the niche-bias
For start-biased layout we want to avoid overpromoting so that
the niche doesn't get pushed back.
For end-biased layout we want to avoid promoting fields that
may contain one of the niches of interest.
2023-04-28 23:08:54 +02:00
The 8472
afe106cdc8 [review] add comments, turn flag into enum 2023-04-28 23:08:54 +02:00
The 8472
351e208f4c add tracing for layout optimizations 2023-04-27 22:29:03 +02:00
The 8472
4907dac54c don't promote large fields to higher alignments if that would affect niche placement 2023-04-27 22:29:03 +02:00
The 8472
faf2da3e2f try two different niche-placement strategies when layouting univariant structs 2023-04-27 22:29:03 +02:00
The 8472
be8e67d93c refactor: extract function 2023-04-27 22:29:03 +02:00
Maybe Waffle
e496fbec92 Split {Idx, IndexVec, IndexSlice} into their own modules 2023-04-24 13:53:35 +00:00
Ben Kimock
0445fbdd83 Store hashes in special types so they aren't accidentally encoded as numbers 2023-04-18 10:52:47 -04:00
Matthias Krüger
0790996a07
Rollup merge of #110394 - scottmcm:less-idx-new, r=WaffleLapkin
Various minor Idx-related tweaks

Nothing particularly exciting here, but a couple of things I noticed as I was looking for more index conversions to simplify.

cc https://github.com/rust-lang/compiler-team/issues/606
r? `@WaffleLapkin`
2023-04-17 18:13:35 +02:00
Scott McMurray
99fd9cb697 Remove the loop in Align::from_bytes
Perf is almost certainly irrelevant, but might as well simplify it, since `trailing_zeros` does exactly what's needed.
2023-04-16 04:01:34 -07:00
Scott McMurray
c98895d9f2 Various minor Idx-related tweaks
Nothing particularly exciting here, but a couple of things I noticed as I was looking for more index conversions to simplify.
2023-04-16 02:42:50 -07:00
Nilstrieb
81c320ea77 Fix some clippy::complexity 2023-04-09 23:22:14 +02:00
Michael Goulet
920c51c526 Enforce that PointerLike requires a pointer-like ABI 2023-04-08 21:11:16 +00:00
Scott McMurray
21bb8ef24e Use FieldIdx in FieldsShape
Finally got to the main motivating example from the MCP :)
2023-04-04 12:38:06 -07:00
Scott McMurray
a2ee7592d6 Use &IndexSlice instead of &IndexVec where possible
All the same reasons as for `[T]`: more general, less pointer chasing, and `&mut IndexSlice` emphasizes that it doesn't change *length*.
2023-04-02 17:35:37 -07:00
Scott McMurray
5bbaeadc01 Move mir::Fieldabi::FieldIdx
The first PR for https://github.com/rust-lang/compiler-team/issues/606

This is just the move-and-rename, because it's plenty big-and-bitrotty already.  Future PRs will start using `FieldIdx` more broadly, and concomitantly removing `FieldIdx::new`s.
2023-03-28 22:22:37 -07:00
Scott McMurray
0439d13176 Refactor: VariantIdx::from_u32(0) -> FIRST_VARIANT
Since structs are always `VariantIdx(0)`, there's a bunch of files where the only reason they had `VariantIdx` or `vec::Idx` imported at all was to get the first variant.

So this uses a constant for that, and adds some doc-comments to `VariantIdx` while I'm there, since it doesn't have any today.
2023-03-25 18:58:25 -07:00
Nilstrieb
025d2a147f Unify validity checks into a single query
Previously, there were two queries to check whether a type allows the
0x01 or zeroed bitpattern.

I am planning on adding a further initness to check, truly uninit for
MaybeUninit, which would make this three queries. This seems overkill
for such a small feature, so this PR unifies them into one.
2023-02-23 18:42:36 +00:00
Matthias Krüger
e0aa5613d8
Rollup merge of #107592 - workingjubilee:use-16-bit-enum-on-16-bit-targets, r=WaffleLapkin
Default `repr(C)` enums to `c_int` size

This is what ISO C strongly implies this is correct, and
many processor-specific ABIs imply or mandate this size, so
"everyone" (LLVM, gcc...) defaults to emitting enums this way.
However, this is by no means guaranteed by ISO C,
and the bare-metal Arm targets show it can be overridden,
which rustc supports via `c-enum-min-bits` in a target.json.

The override is a flag named `-fshort-enums` in clang and gcc,
but introducing a CLI flag is probably unnecessary for rustc.
This flag can be used by non-Arm microcontroller targets,
like AVR and MSP430, but it is not enabled for them by default.
Rust programmers who know the size of a target's enums
can use explicit reprs, which also lets them match C23 code.

This change is most relevant to 16-bit targets: AVR and MSP430.
Most of rustc's targets use 32-bit ints, but ILP64 does exist.
Regardless, rustc should now correctly handle enums for
both very small and very large targets.

Thanks to William for confirming MSP430 behavior,
and to Waffle for better style and no-core `size_of` asserts.

Fixes rust-lang/rust#107361
Fixes rust-lang/rust#77806
2023-02-17 12:39:05 +01:00
Jubilee Young
2edf6c8784 Default repr(C) enums to c_int size
This is what ISO C strongly implies this is correct, and
many processor-specific ABIs imply or mandate this size, so
"everyone" (LLVM, gcc...) defaults to emitting enums this way.
However, this is by no means guaranteed by ISO C,
and the bare-metal Arm targets show it can be overridden,
which rustc supports via `c-enum-min-bits` in a target.json.

The override is a flag named `-fshort-enums` in clang and gcc,
but introducing a CLI flag is probably unnecessary for rustc.
This flag can be used by non-Arm microcontroller targets,
like AVR and MSP430, but it is not enabled for them by default.
Rust programmers who know the size of a target's enums
can use explicit reprs, which also lets them match C23 code.

This change is most relevant to 16-bit targets: AVR and MSP430.
Most of rustc's targets use 32-bit ints, but ILP64 does exist.
Regardless, rustc should now correctly handle enums for
both very small and very large targets.

Thanks to William for confirming MSP430 behavior,
and to Waffle for better style and no-core size_of asserts.

Co-authored-by: William D. Jones <thor0505@comcast.net>
Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
2023-02-16 15:06:17 -08:00
Dylan DPC
9800dbe883
Rollup merge of #107163 - mikebenfield:parameters-pr, r=TaKO8Ki
Remove some superfluous type parameters from layout.rs.

Specifically remove V, which can always be VariantIdx, and F, which can always be Layout.
2023-02-15 12:24:54 +05:30
Ralf Jung
1ef16874b5 also do not add noalias on not-Unpin Box 2023-02-06 12:17:41 +01:00
Ralf Jung
201ae73872 make PointerKind directly reflect pointer types
The code that consumes PointerKind (`adjust_for_rust_scalar` in rustc_ty_utils)
ended up using PointerKind variants to talk about Rust reference types (& and
&mut) anyway, making the old code structure quite confusing: one always had to
keep in mind which PointerKind corresponds to which type. So this changes
PointerKind to directly reflect the type.

This does not change behavior.
2023-02-06 11:46:32 +01:00
Ralf Jung
c8e8d6e7f6 PointeeInfo is advisory only 2023-01-31 18:49:03 +01:00
Erik Desjardins
009192b01b abi: add AddressSpace field to Primitive::Pointer
...and remove it from `PointeeInfo`, which isn't meant for this.

There are still various places (marked with FIXMEs) that assume all pointers
have the same size and alignment. Fixing this requires parsing non-default
address spaces in the data layout string, which will be done in a followup.
2023-01-22 23:41:39 -05:00
Erik Desjardins
96f8f99589 rustc_abi: remove Primitive::{is_float,is_int}
there were fixmes for this already

i am about to remove is_ptr (since callers need to properly distinguish
between pointers in different address spaces), so might as well do this
at the same time
2023-01-22 21:02:07 -05:00
Michael Benfield
8df27d07ae Remove some superfluous type parameters from layout.rs.
Specifically remove V, which can always be VariantIdx, and F, which can
always be Layout.
2023-01-21 10:22:31 -08:00
Matthias Krüger
68f12338af
Rollup merge of #104505 - WaffleLapkin:no-double-spaces-in-comments, r=jackh726
Remove double spaces after dots in comments

Most of the comments do not have double spaces, so I assume these are typos.
2023-01-17 20:21:25 +01:00
Maybe Waffle
6a28fb42a8 Remove double spaces after dots in comments 2023-01-17 08:09:33 +00:00
André Vennberg
da3623abab Removed various double spaces in compiler source comments. 2023-01-14 17:34:59 +01:00