Commit graph

2829 commits

Author SHA1 Message Date
Pavel Grigorenko
b86553f808 Stabilize alloc_layout_extra 2026-01-11 16:39:18 +03:00
Martin Nordholts
0cba97b3b4 Finish transition from semitransparent to semiopaque for rustc_macro_transparency 2026-01-08 19:14:45 +01:00
bjorn3
6da1048236 Fix some divergences with the cg_clif subtree
For some reason git-subtree incorrectly synced those changes.
2025-12-24 15:16:59 +00:00
bors
fcdea4d663 Auto merge of #149114 - BoxyUwU:mgca_adt_exprs, r=lcnr
MGCA: Support struct expressions without intermediary anon consts

r? oli-obk

tracking issue: rust-lang/rust#132980

Fixes rust-lang/rust#127972
Fixes rust-lang/rust#137888
Fixes rust-lang/rust#140275

due to delaying a bug instead of ICEing in HIR ty lowering.

### High level goal

Under `feature(min_generic_const_args)` this PR adds another kind of const argument. A struct/variant construction const arg kind. We represent the values of the fields as themselves being const arguments which allows for uses of generic parameters subject to the existing restrictions present in `min_generic_const_args`:
```rust
fn foo<const N: Option<u32>>() {}

trait Trait {
    #[type_const]
    const ASSOC: usize;
}

fn bar<T: Trait, const N: u32>() {
    // the initializer of `_0` is a `N` which is a legal const argument
    // so this is ok.
    foo::<{ Some::<u32> { 0: N } }>();

    // this is allowed as mgca supports uses of assoc consts in the
    // type system. ie `<T as Trait>::ASSOC` is a legal const argument
    foo::<{ Some::<u32> { 0: <T as Trait>::ASSOC } }>();

    // this on the other hand is not allowed as `N + 1` is not a legal
    // const argument
    foo::<{ Some::<u32> { 0: N + 1 } }>();
}
```

This PR does not support uses of const ctors, e.g. `None`. And also does not support tuple constructors, e.g. `Some(N)`. I believe that it would not be difficult to add support for such functionality after this PR lands so have left it out deliberately.

We currently require that all generic parameters on the type being constructed be explicitly specified. I haven't really looked into why that is but it doesn't seem desirable to me as it should be legal to write `Some { ... }` in a const argument inside of a body and have that desugar to `Some::<_> { ... }`. Regardless this can definitely be a follow-up PR and I assume this is some underlying consistency with the way that elided args are handled with type paths elsewhere.

This PRs implementation of supporting struct expressions is somewhat incomplete. We don't handle `Foo { ..expr }` at all and aren't handling privacy/stability. The printing of `ConstArgKind::Struct` HIR nodes doesn't really exist either :')

I've tried to keep the implementation here somewhat deliberately incomplete as I think a number of these issues are actually quite small and self contained after this PR lands and I'm hoping it could be a good set of issues to mentor newer contributors on 🤔 I just wanted the "bare minimum" required to actually demonstrate that the previous changes are "necessary".

### `ValTree` now recurse through `ty::Const`

In order to actually represent struct/variant construction in `ty::Const` without going through an anon const we would need to introduce some new `ConstKind` variant. Let's say some hypothetical `ConstKind::ADT(Ty<'tcx>, List<Const<'tcx>>)`.

This variant would represent things the same way that `ValTree` does with the first element representing the `VariantIdx` of the enum (if its an enum), and then followed by a list of field values in definition order.

This *could* work but there are a few reasons why it's suboptimal.

First it would mean we have a second kind of `Const` that can be normalized. Right now we only have `ConstKind::Unevaluated` which possibly needs normalization. Similarly with `TyKind` we *only* have `TyKind::Alias`. If we introduced `ConstKind::ADT` it would need to be normalized to a `ConstKind::Value` eventually. This feels to me like it has the potential to cause bugs in the long run where only `ConstKind::Unevaluated` is handled by some code paths.

Secondly it would make type equality/inference be kind of... weird... It's desirable for `Some { 0: ?x } eq Some { 0: 1_u32 }` to result in `?x=1_u32`.  I can't see a way for this to work with this `ConstKind::ADT` design under the current architecture for how we represent types/consts and generally do equality operations.

We would need to wholly special case these two variants in type equality and have a custom recursive walker separate from the existing architecture for doing type equality. It would also be somewhat unique in that it's a non-rigid `ty::Const` (it can be normalized more later on in type inference) while also having somewhat "structural" equality behaviour.

Lastly, it's worth noting that its not *actually* `ConstKind::ADT` that we want. It's desirable to extend this setup to also support tuples and arrays, or even references if we wind up supporting those in const generics. Therefore this isn't really `ConstKind::ADT` but a more general `ConstKind::ShallowValue` or something to that effect. It represents at least one "layer" of a types value :')

Instead of doing this implementation choice we instead change `ValTree::Branch`:
```rust
enum ValTree<'tcx> {
    Leaf(ScalarInt),
    // Before this PR:
    Branch(Box<[ValTree<'tcx>]>),
    // After this PR
    Branch(Box<[Const<'tcx>]>),
}
```

The representation for so called "shallow values" is now the same as the representation for the *entire* full value. The desired inference/type equality behaviour just falls right out of this. We also don't wind up with these shallow values actually being non-rigid. And `ValTree` *already* supports references/tuples/arrays so we can handle those just fine.

I think in the future it might be worth considering inlining `ValTree` into `ty::ConstKind`. E.g:
```rust
enum ConstKind {
    Scalar(Ty<'tcx>, ScalarInt),
    ShallowValue(Ty<'tcx>, List<Const<'tcx>>),
    Unevaluated(UnevaluatedConst<'tcx>),
    ...
}
```

This would imply that the usage of `ValTree`s in patterns would now be using `ty::Const` but they already kind of are anyway and I think that's probably okay in the long run. It also would mean that the set of things we *could* represent in const patterns is greater which may be desirable in the long run for supporting things such as const patterns of const generic parameters.

Regardless, this PR doesn't actually inline `ValTree` into `ty::ConstKind`, it only changes `Branch` to recurse through `Const`. This change could be split out of this PR if desired.

I'm not sure if there'll be a perf impact from this change. It's somewhat plausible as now all const pattern values that have nesting will be interning a lot more `Ty`s. We shall see :>

### Forbidding generic parameters under mgca

Under mgca we now allow all const arguments to resolve paths to generic parameters. We then *later* actually validate that the const arg should be allowed to access generic parameters if it did wind up resolving to any.

This winds up just being a lot simpler to implement than trying to make name resolution "keep track" of whether we're inside of a non-anon-const const arg and then encounter a `const { ... }` indicating we should now stop allowing resolving to generic parameters.

It's also somewhat in line with what we'll need for a `feature(generic_const_args)` where we'll want to decide whether an anon const should have any generic parameters based off syntactically whether any generic parameters were used. Though that design is entirely hypothetical at this point :)

### Followup Work

- Make HIR ty lowering check whether lowering generic parameters is supported and if not lower to an error type/const. Should make the code cleaner, fix some other bugs, and maybe(?) recover perf since we'll be accessing less queries which I think is part of the perf regression of this PR
- Make the ValTree setup less scuffed. We should find a new name for `ConstKind::Value` and the `Val` part of `ValTree` and `ty::Value` as they no longer correspond to a fully normalized structure. It may also be worth looking into inlining `ValTreeKind` into `ConstKind` or atleast into `ty::Value` or sth 🤔
- Support tuple constructors and const constructors not just struct expressions.
- Reduce code duplication between HIR ty lowering's handling of struct expressions, and HIR typeck's handling of struct expressions
- Try fix perf https://github.com/rust-lang/rust/pull/149114#issuecomment-3668038853. Maybe this will clear up once we clean up `ValTree` a bit and stop doing double interning and whatnot
2025-12-23 23:53:55 +00:00
bjorn3
4e375db44a Merge commit '6f3f6bdacb' into sync_cg_clif-2025-12-23 2025-12-23 17:47:42 +00:00
Boxy Uwu
50ef284125 Fix tools 2025-12-23 13:55:00 +00:00
Boxy Uwu
1acfdbb3cd Make ValTree recurse through ty::Const 2025-12-23 13:54:59 +00:00
bors
987298c392 Auto merge of #148766 - cjgillot:mir-const-runtime-checks, r=RalfJung,saethlin
Replace Rvalue::NullaryOp by a variant in mir::Operand.

Based on https://github.com/rust-lang/rust/pull/148151

This PR fully removes the MIR `Rvalue::NullaryOp`. After rust-lang/rust#148151, it was only useful for runtime checks like `ub_checks`, `contract_checks` and `overflow_checks`.

These are "runtime" checks, boolean constants that may only be `true` in codegen. It depends on a rustc flag passed to codegen, so we need to represent those flags cross-crate.

This PR replaces those runtime checks by special variants in MIR `ConstValue`. This allows code that expects constants to manipulate those as such, even if we may not always be able to evaluate them to actual scalars.
2025-12-22 06:58:28 +00:00
Moulins
005046ddd4 layout: Store inverse memory index in FieldsShape::Arbitrary
All usages of `memory_index` start by calling `invert_bijective_mapping`, so
storing the inverted mapping directly saves some work and simplifies the code.
2025-12-18 22:25:34 +01:00
bjorn3
45671b42e6 Merge commit '8de4afd39b' into sync_cg_clif-2025-12-18 2025-12-18 11:50:08 +00:00
Stuart Cook
d57ccad63d Rollup merge of #149950 - WaffleLapkin:inlines-ur-mu-into-asm, r=jdonszelmann
Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from https://github.com/rust-lang/rust/pull/149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
2025-12-16 14:40:44 +11:00
Camille Gillot
85921620d1 Use ScalarInt from bool. 2025-12-14 17:29:59 +00:00
Camille Gillot
a3676bd0fa Introduce Operand::RuntimeChecks. 2025-12-14 17:25:53 +00:00
Camille Gillot
a0de5ae5a1 Replace Rvalue::NullaryOp by a variant in mir::ConstValue. 2025-12-14 17:25:51 +00:00
Waffle Lapkin
6a821c5518 simplify how inline asm handles MaybeUninit 2025-12-13 15:50:02 +01:00
bors
956134622e Auto merge of #149709 - Urgau:overhaul-filenames, r=davidtwco
Overhaul filename handling for cross-compiler consistency

This PR overhauls the way we handle filenames in the compiler and `rmeta` in order to achieve achieve cross-compiler consistency (ie. having the same path no matter if the filename was created in the current compiler session or is coming from `rmeta`).

This is required as some parts of the compiler rely on consistent paths for the soundness of generated code (see rust-lang/rust#148328).

In order to achieved consistency multiple steps are being taken by this PR:
 - by making `RealFileName` immutable
 - by only having `SourceMap::to_real_filename` create `RealFileName`
   - currently `RealFileName` can be created from any `Path` and are remapped afterwards, which creates consistency issue
 - by also making `RealFileName` holds it's working directory, embeddable name and the remapped scopes
   - this removes the need for a `Session`, to know the current(!) scopes and cwd, which is invalid as they may not be equal to the scopes used when creating the filename

In order for `SourceMap::to_real_filename` to know which scopes to apply `FilePathMapping` now takes the current remapping scopes to apply, which makes `FileNameDisplayPreference` and company useless and are removed.

This PR is split-up in multiple commits (unfortunately not atomic), but should help review the changes.

Unblocks https://github.com/rust-lang/rust/pull/147611
Fixes https://github.com/rust-lang/rust/issues/148328
2025-12-13 14:32:09 +00:00
Esteban Küber
e3f9bcef74 Use let...else instead of match foo { ... _ => return }; and if let ... else return 2025-12-12 17:52:39 +00:00
Matthias Krüger
2298c01a8b Rollup merge of #149791 - clubby789:cfg-bool-lints, r=jdonszelmann
Remove uses of `cfg({any()/all()})`

~~This implements the followup warning suggested in https://github.com/rust-lang/rfcs/pull/3695~~
~~Lint against an empty `cfg(any/all)`, suggest the boolean literal equivalents.~~
https://github.com/rust-lang/rust/pull/149791#issuecomment-3638624348

Tracking issue: https://github.com/rust-lang/rust/issues/131204
2025-12-12 12:19:09 +01:00
Urgau
d55b61a684 Adapt cg_cranelift to the overhauled filename handling 2025-12-12 07:34:52 +01:00
Jamie Hill-Daniel
932c939c9f Remove uses of cfg(any()/all()) 2025-12-10 23:41:19 +00:00
Matthias Krüger
51e8b6e427 Rollup merge of #149764 - Zalathar:has-zstd, r=bjorn3
Make `--print=backend-has-zstd` work by default on any backend

Using a defaulted `CodegenBackend` method that querying for zstd support should automatically print a safe value of `false` on any backend that doesn't specifically indicate the presence or absence of zstd.

This should fix the compiletest failures reported in https://github.com/rust-lang/rust/pull/149666#discussion_r2597881482, which can occur when LLVM is not the default codegen backend.
2025-12-10 17:16:48 +01:00
Matthias Krüger
77f1582a9e Rollup merge of #147725 - bjorn3:remove_oom_panic, r=Amanieu
Remove -Zoom=panic

There are major questions remaining about the reentrancy that this allows. It doesn't have any users on github outside of a single project that uses it in a panic=abort project to show backtraces. It can still be emulated through `#[alloc_error_handler]` or `set_alloc_error_hook` depending on if you use the standard library or not. And finally it makes it harder to do various improvements to the allocator shim.

With this PR the sole remaining symbol in the allocator shim that is not effectively emulating weak symbols is the symbol that prevents skipping the allocator shim on stable even when it would otherwise be empty because libstd + `#[global_allocator]` is used.

Closes https://github.com/rust-lang/rust/issues/43596
Fixes https://github.com/rust-lang/rust/issues/126683
2025-12-10 07:54:17 +01:00
Zalathar
d38a5a6d1d Make --print=backend-has-zstd work by default on any backend
Using a defaulted `CodegenBackend` method that querying for zstd support should
automatically print a safe value of `false` on any backend that doesn't
specifically indicate the presence or absence of zstd.
2025-12-09 12:57:19 +11:00
bjorn3
66381e3d79 Revert build_llvm_sysroot_for_triple back from reading the manifest to filtering
Reading the manifest doesn't work when running in the context of the
rust build system.
2025-12-08 19:11:10 +00:00
bjorn3
6693a568af Merge commit 'e24f0fa3c5' into sync_cg_clif-2025-12-08 2025-12-08 16:20:48 +00:00
Boxy Uwu
85cf2825f4 account for safe target features in fndef<->closure and fndef<->fndef coerce-lubs 2025-12-03 14:55:41 +00:00
bjorn3
4c29d61ec6 Remove -Zoom=panic
There are major questions remaining about the reentrancy that this
allows. It doesn't have any users on github outside of a single project
that uses it in a panic=abort project to show backtraces. It
can still be emulated through #[alloc_error_handler] or
set_alloc_error_hook depending on if you use the standard library or
not. And finally it makes it harder to do various improvements to the
allocator shim.
2025-11-28 19:30:39 +00:00
Camille Gillot
94ff1de61e Replace OffsetOf by an actual sum. 2025-11-18 00:10:03 +00:00
Tamir Duberstein
d1c33c3e84 rustc_target: introduce Os
Improve type safety by using an enum rather than strings.
2025-11-11 18:55:40 -05:00
Tamir Duberstein
46f27590af rustc_target: introduce Env
Improve type safety by using an enum rather than strings.
2025-11-11 18:34:47 -05:00
Tamir Duberstein
032ba980b1 rustc_target: introduce Abi
Improve type safety by using an enum rather than strings.
2025-11-11 18:22:38 -05:00
Tamir Duberstein
35a99c1d41 rustc_target: hide TargetOptions::vendor 2025-11-09 17:43:36 -05:00
Stuart Cook
5873ee5bb6 Rollup merge of #128666 - pitaj:intrinsic-overflow_checks, r=BoxyUwU
Add `overflow_checks` intrinsic

This adds an intrinsic which allows code in a pre-built library to inherit the overflow checks option from a crate depending on it. This enables code in the standard library to explicitly change behavior based on whether `overflow_checks` are enabled, regardless of the setting used when standard library was compiled.

This is very similar to the `ub_checks` intrinsic, and refactors the two to use a common mechanism.

The primary use case for this is to allow the new `RangeFrom` iterator to yield the maximum element before overflowing, as requested [here](https://github.com/rust-lang/rust/issues/125687#issuecomment-2151118208). This PR includes a working `IterRangeFrom` implementation based on this new intrinsic that exhibits the desired behavior.

[Prior discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Ability.20to.20select.20code.20based.20on.20.60overflow_checks.60.3F)
2025-11-09 13:22:23 +11:00
bjorn3
a44af2ba0a Add missing --check-cfg 2025-11-08 15:04:45 +00:00
bjorn3
c90a9d836b Merge commit 'a0b865dc87' into sync_cg_clif-2025-11-08 2025-11-08 14:18:53 +00:00
bjorn3
2073d1209d Unify the configuration of the compiler docs
Previously it was rather inconsistent which crates got the rust logo and
which didn't and setting html_root_url was forgotten in many cases.
2025-11-05 11:25:27 +00:00
bors
c2d6d4a0b5 Auto merge of #148507 - Zalathar:rollup-vvz4knr, r=Zalathar
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#147355 (Add alignment parameter to `simd_masked_{load,store}`)
 - rust-lang/rust#147925 (Fix tests for big-endian)
 - rust-lang/rust#148341 (compiler: Fix a couple issues around cargo feature unification)
 - rust-lang/rust#148371 (Dogfood `trim_{suffix|prefix}` in compiler)
 - rust-lang/rust#148495 (Implement Path::is_empty)
 - rust-lang/rust#148502 (rustc-dev-guide subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-11-05 07:25:39 +00:00
Tamir Duberstein
f47ad44886 rustc_target: allow unenumerated architectures 2025-11-04 21:28:28 -05:00
Tamir Duberstein
b49a1f7aad rustc_target: introduce Arch
Improve type safety by using an enum rather than strings.
2025-11-04 21:27:22 -05:00
sayantn
d28c5a51c4 Implement the alignment parameter in cg_clif 2025-11-04 03:02:44 +05:30
Peter Jaszkowiak
482feee311 refactor ub_checks and contract_checks to share logic 2025-10-25 14:30:04 -06:00
bjorn3
b548b29e10 Skip codegen_crate call in check mode 2025-10-24 10:25:13 +00:00
Camille Gillot
cc27b04473 Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
bors
0eae3a2bed Auto merge of #147687 - cjgillot:noshallow-init-box, r=nnethercote
Forbid ShallowInitBox after box deref elaboration.

MIR currently contains a `ShallowInitBox` rvalue. Its principal usage is to allow for in-place initialization of boxes. Having it is necessary for drop elaboration to be correct with that in-place initialization.

As part of analysis->runtime MIR lowering, we canonicalize deref of boxes to use the stored raw pointer. But we did not perform the same change to the construction of the box.

This PR replaces `ShallowInitBox` by the pointer manipulation it represents.

Alternatives:
- fully remove `ShallowInitBox` and implement `Box` in-place initialization differently;
- remove the `ElaborateBoxDeref` pass and keep dereferencing `Box` in runtime MIR.
2025-10-22 09:53:50 +00:00
Camille Gillot
9d63e8c135 Elaborate ShallowInitBox too. 2025-10-22 00:52:52 +00:00
Oli Scherer
6031dfdaed Allow unsizing pattern types with pointer base 2025-10-21 11:22:51 +00:00
Matthias Krüger
71068af640 Rollup merge of #145724 - folkertdev:track-caller-drop-no-mangle, r=fee1-dead
the `#[track_caller]` shim should not inherit `#[no_mangle]`

fixes https://github.com/rust-lang/rust/issues/143162

builds on https://github.com/rust-lang/rust/pull/143293 which introduced a mechanism to strip attributes from shims.

cc `@Jules-Bertholet` `@workingjubilee` `@bjorn3`

---

Summary:

This PR fixes an interaction between `#[track_caller]`, `#[no_mangle]`, and casting to a function pointer.

A function annotated with `#[track_caller]` internally has a hidden extra argument for the panic location. The `#[track_caller]` attribute is only allowed on `extern "Rust"` functions. When a function is annotated with both `#[no_mangle]` and `#[track_caller]`, the exported symbol has the signature that includes the extra panic location argument. This works on stable rust today:

```rust
extern "Rust" {
    #[track_caller]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static>;
}

mod provides {
    use std::panic::Location;
    #[track_caller] // UB if we did not have this!
    #[no_mangle]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static> {
        Location::caller()
    }
}
```

When a `#[track_caller]` function is converted to a function pointer, a shim is added to drop the additional argument. So this is a valid program:

```rust
#[track_caller]
fn foo() {}

fn main() {
    let f = foo as fn();
    f();
}
```

The issue arises when `foo` is additionally annotated with `#[no_mangle]`, the generated shim currently inherits this attribute, also exporting a symbol named `foo`, but one without the hidden panic location argument. The linker rightfully complains about a duplicate symbol.

The solution of this PR is to have the generated shim drop the `#[no_mangle]` attribute.
2025-10-18 08:08:36 +02:00
Matthias Krüger
c15f855f5a Rollup merge of #147576 - Mark-Simulacrum:fix-offset-zst, r=nnethercote,RalfJung
Fix ICE on offsetted ZST pointer

I'm not sure this is the *right* fix, but it's simple enough and does roughly what I'd expect. Like with the previous optimization to codegen usize rather than a zero-sized static, there's no guarantee that we continue returning a particular value from the offsetting.

A grep for `const_usize.*align` found the same code copied to rustc_codegen_gcc and cranelift but a quick skim didn't find other cases of similar 'optimization'. That said, I'm not convinced I caught everything, it's not trivial to search for this.

Closes rust-lang/rust#147516
2025-10-16 19:35:24 +02:00
Mark Rousskov
10ee204ff7 Fix ICE on offsetted ZST pointer
A grep for `const_usize.*align` found the same code copied to
rustc_codegen_gcc but I don't see other cases where we get this wrong.
2025-10-15 20:06:46 -04:00
bors
a63c039af8 Auto merge of #143548 - Diggsey:db-limit-extern-crate-usage, r=oli-obk
Restrict sysroot crate imports to those defined in this repo.

It's common to import dependencies from the sysroot via `extern crate` rather than use an explicit cargo dependency, when it's necessary to use the same dependency version as used by rustc itself. However, this is dangerous for crates.io crates, since rustc may not pull in the dependency on some targets, or may pull in multiple versions. In both cases, the `extern crate` fails to resolve.

To address this, re-export all such dependencies from the appropriate `rustc_*` crates, and use this alias from crates which would otherwise need to use `extern crate`.

See https://github.com/rust-lang/rust/pull/143492 for an example of the kind of issue that can occur.
2025-10-15 13:32:03 +00:00