Extend well-formedness checking and HIR analysis to prohibit the use of
scalable vectors in structs, enums, unions, tuples and arrays. LLVM does
not support scalable vectors being members of other types, so these
restrictions are necessary.
Co-authored-by: Jamie Cunliffe <Jamie.Cunliffe@arm.com>
Externally implementable items
Supersedes https://github.com/rust-lang/rust/pull/140010
Tracking issue: https://github.com/rust-lang/rust/issues/125418
Getting started:
```rust
#![feature(eii)]
#[eii(eii1)]
pub fn decl1(x: u64)
// body optional (it's the default)
{
println!("default {x}");
}
// in another crate, maybe
#[eii1]
pub fn decl2(x: u64) {
println!("explicit {x}");
}
fn main() {
decl1(4);
}
```
- tiny perf regression, underlying issue makes multiple things in the compiler slow, not just EII, planning to solve those separately.
- No codegen_gcc support, they don't have bindings for weak symbols yet but could
- No windows support yet for weak definitions
This PR merges the implementation of EII for just llvm + not windows, doesn't yet contain like a new panic handler implementation or alloc handler. With this implementation, it would support implementing the panic handler in terms of EII already since it requires no default implementation so no weak symbols
The PR has been open in various forms for about a year now, but I feel that having some implementation merged to build upon
Move `struct Placeholder<T>`
r? ghost
Couple of issues I've encountered;
- `compiler/rustc_infer/src/infer/region_constraints/mod.rs` `GenericKind` I can't call `write!(f, "{p}")` due to error 1. Which looks like I may need to implement `Lift` for `Placeholder`?
- Using the `define_print_and_forward_display!` for `ty::PlaceholderType` caused error 2, as I've moved the struct it no longer exists in the crate. I suspect because I'm not using that macro it causes the error for `GenericKind`
<details>
<summary>Error 1</summary>
```
error: lifetime may not live long enough
--> compiler/rustc_infer/src/infer/region_constraints/mod.rs:672:38
|
668 | impl<'tcx> fmt::Display for GenericKind<'tcx> {
| ---- lifetime `'tcx` defined here
...
672 | GenericKind::Placeholder(ref p) => write!(f, "{p}"),
| ^^^^^ assignment requires that `'tcx` must outlive `'static`
|
= note: requirement occurs because of the type `rustc_middle::ty::TyCtxt<'_>`, which makes the generic argument `'_` invariant
= note: the struct `rustc_middle::ty::TyCtxt<'tcx>` is invariant over the parameter `'tcx`
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error: implementation of `Lift` is not general enough
--> compiler/rustc_infer/src/infer/region_constraints/mod.rs:672:38
|
672 | GenericKind::Placeholder(ref p) => write!(f, "{p}"),
| ^^^^^ implementation of `Lift` is not general enough
|
= note: `Lift<rustc_middle::ty::TyCtxt<'0>>` would have to be implemented for the type `rustc_type_ir::Placeholder<rustc_middle::ty::TyCtxt<'_>, BoundTy>`, for any lifetime `'0`...
= note: ...but `Lift<rustc_middle::ty::TyCtxt<'1>>` is actually implemented for the type `rustc_type_ir::Placeholder<rustc_middle::ty::TyCtxt<'1>, BoundTy>`, for some specific lifetime `'1`
error: implementation of `Print` is not general enough
--> compiler/rustc_infer/src/infer/region_constraints/mod.rs:672:38
|
672 | GenericKind::Placeholder(ref p) => write!(f, "{p}"),
| ^^^^^ implementation of `Print` is not general enough
```
</details>
<details>
<summary>Error 2</summary>
```
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> compiler/rustc_middle/src/ty/print/pretty.rs:3060:38
|
3057 | / macro_rules! forward_display_to_print {
3058 | | ($($ty:ty),+) => {
3059 | | // Some of the $ty arguments may not actually use 'tcx
3060 | | $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
... |
3072 | | };
3073 | | }
| |_- in this expansion of `forward_display_to_print!`
...
3093 | / forward_display_to_print! {
3094 | | ty::Region<'tcx>,
3095 | | Ty<'tcx>,
3096 | | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3097 | | ty::Const<'tcx>,
3098 | | &'tcx ty::PlaceholderType<'tcx>
| | ------------------------------- `rustc_type_ir::Placeholder` is not defined in the current crate
3099 | | }
| |_- in this macro invocation
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: implementation of `Lift` is not general enough
--> compiler/rustc_middle/src/ty/print/pretty.rs:3060:38
|
3057 | / macro_rules! forward_display_to_print {
3058 | | ($($ty:ty),+) => {
3059 | | // Some of the $ty arguments may not actually use 'tcx
3060 | | $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Lift` is not general enough
... |
3072 | | };
3073 | | }
| |_- in this expansion of `forward_display_to_print!`
...
3093 | / forward_display_to_print! {
3094 | | ty::Region<'tcx>,
3095 | | Ty<'tcx>,
3096 | | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3097 | | ty::Const<'tcx>,
3098 | | &'tcx ty::PlaceholderType<'tcx>
3099 | | }
| |_- in this macro invocation
|
= note: `Lift<context::TyCtxt<'0>>` would have to be implemented for the type `rustc_type_ir::Placeholder<context::TyCtxt<'tcx>, BoundTy>`, for any lifetime `'0`...
= note: ...but `Lift<context::TyCtxt<'1>>` is actually implemented for the type `rustc_type_ir::Placeholder<context::TyCtxt<'1>, BoundTy>`, for some specific lifetime `'1`
error: implementation of `print::Print` is not general enough
--> compiler/rustc_middle/src/ty/print/pretty.rs:3060:38
|
3057 | / macro_rules! forward_display_to_print {
3058 | | ($($ty:ty),+) => {
3059 | | // Some of the $ty arguments may not actually use 'tcx
3060 | | $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `print::Print` is not general enough
... |
3072 | | };
3073 | | }
| |_- in this expansion of `forward_display_to_print!`
...
3093 | / forward_display_to_print! {
3094 | | ty::Region<'tcx>,
3095 | | Ty<'tcx>,
3096 | | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3097 | | ty::Const<'tcx>,
3098 | | &'tcx ty::PlaceholderType<'tcx>
3099 | | }
| |_- in this macro invocation
|
= note: `print::Print<'0, print::pretty::FmtPrinter<'a, '0>>` would have to be implemented for the type `rustc_type_ir::Placeholder<context::TyCtxt<'tcx>, BoundTy>`, for any lifetime `'0`...
= note: ...but `print::Print<'1, print::pretty::FmtPrinter<'a, 'tcx>>` is actually implemented for the type `rustc_type_ir::Placeholder<context::TyCtxt<'1>, BoundTy>`, for some specific lifetime `'1`
error: specializing impl repeats parameter `'tcx`
--> compiler/rustc_middle/src/ty/print/pretty.rs:3060:38
|
3057 | / macro_rules! forward_display_to_print {
3058 | | ($($ty:ty),+) => {
3059 | | // Some of the $ty arguments may not actually use 'tcx
3060 | | $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
... |
3072 | | };
3073 | | }
| |_- in this expansion of `forward_display_to_print!`
...
3093 | / forward_display_to_print! {
3094 | | ty::Region<'tcx>,
3095 | | Ty<'tcx>,
3096 | | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3097 | | ty::Const<'tcx>,
3098 | | &'tcx ty::PlaceholderType<'tcx>
3099 | | }
| |_- in this macro invocation
error[E0277]: the trait bound `&Placeholder<TyCtxt<'tcx>, BoundTy>: Lift<...>` is not satisfied
--> compiler/rustc_middle/src/ty/print/pretty.rs:3064:30
|
3057 | / macro_rules! forward_display_to_print {
3058 | | ($($ty:ty),+) => {
3059 | | // Some of the $ty arguments may not actually use 'tcx
3060 | | $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
... |
3064 | | tcx.lift(*self)
| | ---- ^^^^^ unsatisfied trait bound
| | |
| | required by a bound introduced by this call
... |
3072 | | };
3073 | | }
| |_- in this expansion of `forward_display_to_print!`
...
3093 | / forward_display_to_print! {
3094 | | ty::Region<'tcx>,
3095 | | Ty<'tcx>,
3096 | | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3097 | | ty::Const<'tcx>,
3098 | | &'tcx ty::PlaceholderType<'tcx>
3099 | | }
| |_- in this macro invocation
|
= help: the trait `Lift<context::TyCtxt<'_>>` is not implemented for `&rustc_type_ir::Placeholder<context::TyCtxt<'tcx>, BoundTy>`
note: required by a bound in `context::TyCtxt::<'tcx>::lift`
--> compiler/rustc_middle/src/ty/context.rs:1807:20
|
1807 | pub fn lift<T: Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted> {
| ^^^^^^^^^^^^^^^^^^ required by this bound in `TyCtxt::<'tcx>::lift`
= note: the full name for the type has been written to '/home/jambar02/Documents/arm/rust/build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-unknown-linux-gnu/release/deps/rustc_middle-16e5c44041028d8b.long-type-3843651570422266958.txt'
= note: consider using `--verbose` to print the full type name to the console
help: consider dereferencing here
|
3064 | tcx.lift(**self)
| +
error[E0282]: type annotations needed
--> compiler/rustc_middle/src/ty/print/pretty.rs:3064:21
|
3057 | / macro_rules! forward_display_to_print {
3058 | | ($($ty:ty),+) => {
3059 | | // Some of the $ty arguments may not actually use 'tcx
3060 | | $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
... |
3064 | |/ tcx.lift(*self)
3065 | || .expect("could not lift for printing")
| ||______________________________________________________________^ cannot infer type
... |
3072 | | };
3073 | | }
| |__- in this expansion of `forward_display_to_print!`
...
3093 | / forward_display_to_print! {
3094 | | ty::Region<'tcx>,
3095 | | Ty<'tcx>,
3096 | | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3097 | | ty::Const<'tcx>,
3098 | | &'tcx ty::PlaceholderType<'tcx>
3099 | | }
| |_- in this macro invocation
```
</details>
Deduplicate higher-ranked lifetime capture errors in impl Trait
Previously, when an `impl Trait` captured multiple higher-ranked
lifetimes from an outer `impl Trait`, the compiler would emit a
separate error for each captured lifetime. This resulted in verbose
and confusing diagnostics, especially in edition 2024 where implicit
captures caused duplicate errors.
This commit introduces error accumulation that collects all capture
spans and lifetime declaration spans, then emits a single consolidated
diagnostic using MultiSpan. The new error shows all captured lifetimes
with visual indicators and lists all declarations in a single note.
r? `@estebank`
Fixes https://github.com/rust-lang/rust/issues/143022
cmse: do not calculate the layout of a type with infer types
tracking issue: https://github.com/rust-lang/rust/issues/81391
tracking issue: https://github.com/rust-lang/rust/issues/75835
fixes https://github.com/rust-lang/rust/issues/130104
Don't calculate the layout of a type with an infer type (`_`). This now emits `LayoutError::Unknown`, causing an error similar to when any other calling convention is used in this location.
The tests use separate functions because only the first such error in a function body is reported.
r? `@davidtwco` (might need some T-types assistance)
Add a diagnostic attribute for special casing const bound errors for non-const impls
Somewhat of a follow-up to https://github.com/rust-lang/rust/pull/144194
My plan is to resolve
f4e19c6878/compiler/rustc_hir_typeck/src/callee.rs (L907-913)
but doing so without being able to mark impls the way I do in this PR wrould cause all nice diagnostics about for loops and pointer comparisons to just be a `*const u32 does not implement [const] PartialEq` errors.
Inherent const impl
Some constifications are annoying because we need to repeat `T: Trait` bounds from an impl block on the individual constified `const fn`s as `T: [const] Trait`. We've brainstormed solutions before, and one would be to have separate `const impl` blocks or sth. However the final syntax will look, I decided to just impl this syntax and either have sth nice on nightly to work with or at least move the discussion along.
Also interacts with the discussion around `impl const Trait for Type` vs `const impl Trait for Type`, as we may want to use the latter to keep inherent and trait impls in sync (unless we come up with even another scheme).
* [ ] rustdoc + tests
* [ ] macro stability /regression tests
r? `@fee1-dead`
cc `@traviscross` `@rust-lang/project-const-traits`
repr(transparent) check: do not compute check_unsuited more than once
`field_infos` is an iterator that we execute multiple times. However, we usually ignore the `unsuited` field -- we only need it in the last iteration. So move the computation of that field to that iteration to avoid computing it multiple times. Computing `unsuited` involves a recursive traversal over the types of all non-trivial fields, so there can be non-trivial amounts of work here.
(I benchmarked this in https://github.com/rust-lang/rust/pull/148243 and saw no changes, probably because we don't have a benchmark with many repr(transparent) types. But still, computing this each time just seemed silly.)
cleanup: merge `RvalueScopes` into `ScopeTree`
This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf.
I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang/rust#146098 gets rid of it.
Pulled out from rust-lang/rust#146098.
r? BoxyUwU as the reviewer of rust-lang/rust#146098 (though feel free to reassign/claim! this is just cleanup)
cc `@dingxiangfei2009`
mgca: Finish implementation of `#[type_const]`
tracking issue: rust-lang/rust#132980fixesrust-lang/rust#140729fixesrust-lang/rust#140860
- **Fix `#[type_const]` attribute placement validation**
- **Perform WF-checking on type_const RHS's**
- **Check type_const type is ConstParamTy_ and that RHS matches it**
- **Check that impls of `#[type_const]` consts also have the attr**
r? ```@BoxyUwU```
Remove `#[const_trait]`
Remove `#[const_trait]` since we now have `const trait`. Update all structured diagnostics that still suggested the attribute.
r? ```@rust-lang/project-const-traits```