about summary refs log tree commit diff
path: root/compiler/rustc_middle/src
AgeCommit message (Collapse)AuthorLines
2024-12-10Rollup merge of #134008 - jswrenn:unsafe-fields-copy, r=compiler-errorsLeón Orell Valerian Liehr-5/+10
Make `Copy` unsafe to implement for ADTs with `unsafe` fields As a rule, the application of `unsafe` to a declaration requires that use-sites of that declaration also entail `unsafe`. For example, a field declared `unsafe` may only be read in the lexical context of an `unsafe` block. For nearly all safe traits, the safety obligations of fields are explicitly discharged when they are mentioned in method definitions. For example, idiomatically implementing `Clone` (a safe trait) for a type with unsafe fields will require `unsafe` to clone those fields. Prior to this commit, `Copy` violated this rule. The trait is marked safe, and although it has no explicit methods, its implementation permits reads of `Self`. This commit resolves this by making `Copy` conditionally safe to implement. It remains safe to implement for ADTs without unsafe fields, but unsafe to implement for ADTs with unsafe fields. Tracking: #132922 r? ```@compiler-errors```
2024-12-10Rollup merge of #134029 - Zalathar:zero, r=oli-obkLeón Orell Valerian Liehr-30/+41
coverage: Use a query to find counters/expressions that must be zero As of #133446, this query (`coverage_ids_info`) determines which counter/expression IDs are unused. So with only a little extra work, we can take the code that was using that information to determine which coverage counters/expressions must be zero, and move that inside the query as well. There should be no change in compiler output.
2024-12-10Rollup merge of #134010 - RalfJung:promoted-type-error-ice, r=oli-obkLeón Orell Valerian Liehr-19/+29
fix ICE on type error in promoted Fixes https://github.com/rust-lang/rust/issues/133968 Ensure that when we turn a type error into a "this promoted failed to evaluate" error, we do record this as something that may happen even in "infallible" promoteds.
2024-12-10Auto merge of #134096 - fmease:rollup-0asgoo8, r=fmeasebors-1/+1
Rollup of 9 pull requests Successful merges: - #133996 (Move most tests for `-l` and `#[link(..)]` into `tests/ui/link-native-libs`) - #134012 (Grammar fixes) - #134032 (docs: better examples for `std::ops::ControlFlow`) - #134040 (bootstrap: print{ln}! -> eprint{ln}! (take 2)) - #134043 (Add test to check unicode identifier version) - #134053 (rustdoc: rename `issue-\d+.rs` tests to have meaningful names (part 10)) - #134055 (interpret: clean up deduplicating allocation functions) - #134073 (dataflow_const_prop: do not eval a ptr address in SwitchInt) - #134084 (Fix typo in RFC mention 3598 -> 3593) r? `@ghost` `@rustbot` modify labels: rollup
2024-12-09Rollup merge of #134055 - RalfJung:interpret-alloc-dedup, r=oli-obkLeón Orell Valerian Liehr-1/+1
interpret: clean up deduplicating allocation functions The "align" and "kind" arguments would be largely ignored in the "dedup" case, so let's move that to entirely separate function. Let's also remove support for old-style miri_resolve_frame while we are at it. The docs have already said for a while that this must be set to 1.
2024-12-09review comments: rewordingsEsteban Küber-2/+2
2024-12-09Introduce `default_field_values` featureEsteban Küber-8/+23
Initial implementation of `#[feature(default_field_values]`, proposed in https://github.com/rust-lang/rfcs/pull/3681. Support default fields in enum struct variant Allow default values in an enum struct variant definition: ```rust pub enum Bar { Foo { bar: S = S, baz: i32 = 42 + 3, } } ``` Allow using `..` without a base on an enum struct variant ```rust Bar::Foo { .. } ``` `#[derive(Default)]` doesn't account for these as it is still gating `#[default]` only being allowed on unit variants. Support `#[derive(Default)]` on enum struct variants with all defaulted fields ```rust pub enum Bar { #[default] Foo { bar: S = S, baz: i32 = 42 + 3, } } ``` Check for missing fields in typeck instead of mir_build. Expand test with `const` param case (needs `generic_const_exprs` enabled). Properly instantiate MIR const The following works: ```rust struct S<A> { a: Vec<A> = Vec::new(), } S::<i32> { .. } ``` Add lint for default fields that will always fail const-eval We *allow* this to happen for API writers that might want to rely on users' getting a compile error when using the default field, different to the error that they would get when the field isn't default. We could change this to *always* error instead of being a lint, if we wanted. This will *not* catch errors for partially evaluated consts, like when the expression relies on a const parameter. Suggestions when encountering `Foo { .. }` without `#[feature(default_field_values)]`: - Suggest adding a base expression if there are missing fields. - Suggest enabling the feature if all the missing fields have optional values. - Suggest removing `..` if there are no missing fields.
2024-12-09fix ICE on type error in promotedRalf Jung-19/+29
2024-12-09interpret: clean up deduplicating allocation functionsRalf Jung-1/+1
2024-12-09Rollup merge of #133567 - bjorn3:various_cleanups, r=cjgillotMatthias Krüger-16/+11
A bunch of cleanups These are all extracted from a branch I have to get rid of driver queries. Most of the commits are not directly necessary for this, but were found in the process of implementing the removal of driver queries. Previous PR: https://github.com/rust-lang/rust/pull/132410
2024-12-08coverage: Use a query to find counters/expressions that must be zeroZalathar-1/+12
This query (`coverage_ids_info`) already determines which counter/expression IDs are unused, so it only takes a little extra effort to also determine which counters/expressions must have a value of zero.
2024-12-08coverage: Move `CoverageIdsInfo` into `mir::coverage`Zalathar-30/+30
2024-12-07Tweak wordingEsteban Küber-5/+5
2024-12-07Do not talk about "trait `<Foo = Bar>`"Esteban Küber-2/+4
Pass in an appropriate `Option<DefId>` in more cases from hir ty lowering.
2024-12-07Account for `impl Trait` in "add bound" suggestion messageEsteban Küber-0/+6
2024-12-07Mention type parameter in more cases and don't suggest ~const bound already ↵Esteban Küber-16/+9
there
2024-12-07Use trait name instead of full constraint in suggestion messageEsteban Küber-23/+61
``` help: consider restricting type parameter `T` with traits `Copy` and `Trait` | LL | fn duplicate_custom<T: Copy + Trait>(t: S<T>) -> (S<T>, S<T>) { | ++++++++++++++ ``` ``` help: consider restricting type parameter `V` with trait `Copy` | LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V { | +++++++++++++++++++ ```
2024-12-07Add test for lack of suggestion in stableEsteban Küber-2/+2
This test will break when `Step` gets stabilized, but punt until then.
2024-12-07reword trait bound suggestion message to include the boundsEsteban Küber-14/+31
2024-12-07Don't suggest restricting bound with unstable traits on stableEsteban Küber-8/+24
On nightly, we mention the trait is unstable ``` error[E0277]: the trait bound `T: Unstable` is not satisfied --> $DIR/unstable-trait-suggestion.rs:13:9 | LL | foo(t) | --- ^ the trait `Unstable` is not implemented for `T` | | | required by a bound introduced by this call | note: required by a bound in `foo` --> $DIR/unstable-trait-suggestion.rs:9:11 | LL | fn foo<T: Unstable>(_: T) {} | ^^^^^^^^ required by this bound in `foo` help: consider restricting type parameter `T` but it is an `unstable` trait | LL | pub fn demo<T: Unstable>(t: T) { | ++++++++++ ``` On stable, we don't suggest the trait at all ``` error[E0277]: the trait bound `T: Unstable` is not satisfied --> $DIR/unstable-trait-suggestion.rs:13:9 | LL | foo(t) | --- ^ the trait `Unstable` is not implemented for `T` | | | required by a bound introduced by this call | note: required by a bound in `foo` --> $DIR/unstable-trait-suggestion.rs:9:11 | LL | fn foo<T: Unstable>(_: T) {} | ^^^^^^^^ required by this bound in `foo` ```
2024-12-07Make `Copy` unsafe to implement for ADTs with `unsafe` fieldsJack Wrenn-5/+10
As a rule, the application of `unsafe` to a declaration requires that use-sites of that declaration also require `unsafe`. For example, a field declared `unsafe` may only be read in the lexical context of an `unsafe` block. For nearly all safe traits, the safety obligations of fields are explicitly discharged when they are mentioned in method definitions. For example, idiomatically implementing `Clone` (a safe trait) for a type with unsafe fields will require `unsafe` to clone those fields. Prior to this commit, `Copy` violated this rule. The trait is marked safe, and although it has no explicit methods, its implementation permits reads of `Self`. This commit resolves this by making `Copy` conditionally safe to implement. It remains safe to implement for ADTs without unsafe fields, but unsafe to implement for ADTs with unsafe fields. Tracking: #132922
2024-12-07Auto merge of #133978 - matthiaskrgr:rollup-6gh1iho, r=matthiaskrgrbors-0/+12
Rollup of 7 pull requests Successful merges: - #130209 (Stabilize `std::io::ErrorKind::CrossesDevices`) - #130254 (Stabilize `std::io::ErrorKind::QuotaExceeded`) - #132187 (Add Extend impls for tuples of arity 1 through 12) - #133875 (handle `--json-output` properly) - #133934 (Do not implement unsafe auto traits for types with unsafe fields) - #133954 (Hide errors whose suggestions would contain error constants or types) - #133960 (rustdoc: remove eq for clean::Attributes) r? `@ghost` `@rustbot` modify labels: rollup
2024-12-06Remove polymorphizationBen Kimock-124/+4
2024-12-06Rollup merge of #133934 - jswrenn:unsafe-fields-auto-traits, r=compiler-errorsMatthias Krüger-0/+12
Do not implement unsafe auto traits for types with unsafe fields If a type has unsafe fields, its safety invariants are not simply the conjunction of its field types' safety invariants. Consequently, it's invalid to reason about the safety properties of these types in a purely structural manner — i.e., the manner in which `auto` traits are implemented. Consequently, auto implementations of unsafe auto traits should not be generated for types with unsafe fields. Tracking: #132922 r? `@compiler-errors`
2024-12-06Remove all threading through of ErrorGuaranteed from the driverbjorn3-1/+1
It was inconsistently done (sometimes even within a single function) and most of the rest of the compiler uses fatal errors instead, which need to be caught using catch_with_exit_code anyway. Using fatal errors instead of ErrorGuaranteed everywhere in the driver simplifies things a bit.
2024-12-06Remove 'tcx lifetime from OnDiskCachebjorn3-15/+10
2024-12-06Rollup merge of #133211 - Strophox:miri-correct-state-update-ffi, r=RalfJungMatthias Krüger-0/+50
Extend Miri to correctly pass mutable pointers through FFI Based off of https://github.com/rust-lang/rust/pull/129684, this PR further extends Miri to execute native calls that make use of pointers to *mutable* memory. We adapt Miri's bookkeeping of internal state upon any FFI call that gives external code permission to mutate memory. Native code may now possibly write and therefore initialize and change the pointer provenance of bytes it has access to: Such memory is assumed to be *initialized* afterwards and bytes are given *arbitrary (wildcard) provenance*. This enables programs that correctly use mutating FFI calls to run Miri without errors, at the cost of possibly missing Undefined Behaviour caused by incorrect usage of mutating FFI. > <details> > > <summary> Simple example </summary> > > ```rust > extern "C" { > fn init_int(ptr: *mut i32); > } > > fn main() { > let mut x = std::mem::MaybeUninit::<i32>::uninit(); > let x = unsafe { > init_int(x.as_mut_ptr()); > x.assume_init() > }; > > println!("C initialized my memory to: {x}"); > } > ``` > ```c > void init_int(int *ptr) { > *ptr = 42; > } > ``` > should now show `C initialized my memory to: 42`. > > </details> r? ``@RalfJung``
2024-12-06Rollup merge of #130777 - azhogin:azhogin/reg-struct-return, r=workingjubileeMatthias Krüger-1/+4
rust_for_linux: -Zreg-struct-return commandline flag for X86 (#116973) Command line flag `-Zreg-struct-return` for X86 (32-bit) for rust-for-linux. This flag enables the same behavior as the `abi_return_struct_as_int` target spec key. - Tracking issue: https://github.com/rust-lang/rust/issues/116973
2024-12-05do not implement unsafe auto traits for types with unsafe fieldsJack Wrenn-0/+12
If a type has unsafe fields, its safety invariants are not simply the conjunction of its field types' safety invariants. Consequently, it's invalid to reason about the safety properties of these types in a purely structural manner — i.e., the manner in which `auto` traits are implemented. Makes progress towards #132922.
2024-12-05Rollup merge of #133607 - WaffleLapkin:tail-call-checks, r=compiler-errorsGuillaume Gomez-0/+6
implement checks for tail calls Quoting the [RFC draft](https://github.com/phi-go/rfcs/blob/guaranteed-tco/text/0000-explicit-tail-calls.md): > The argument to become is a function (or method) call, that exactly matches the function signature and calling convention of the callee. The intent is to ensure a matching ABI. Note that lifetimes may differ as long as they pass borrow checking, see [below](https://github.com/phi-go/rfcs/blob/guaranteed-tco/text/0000-explicit-tail-calls.md#return-type-coercion) for specifics on the return type. > Tail calling closures and tail calling from closures is not allowed. This is due to the high implementation effort, see below, this restriction can be lifted by a future RFC. > Invocations of operators were considered as valid targets but were rejected on grounds of being too error-prone. In any case, these can still be called as methods. > Tail calling [variadic functions](https://doc.rust-lang.org/beta/unstable-book/language-features/c-variadic.html) and tail calling from variadic functions is not allowed. As support for variadic function is stabilized on a per target level, support for tail-calls regarding variadic functions would need to follow a similar approach. To avoid this complexity and to minimize implementation effort for backends, this interaction is currently not allowed but support can be added with a future RFC. ----- The checks are implemented as a query, similarly to `check_unsafety`. The code is cherry-picked straight out of #112657 which was written more than a year ago, so I expect we might need to change some things ^^"
2024-12-05extend Miri to correctly pass mutable pointers through FFIStrophox-0/+50
Co-authored-by: Ralf Jung <post@ralfj.de>
2024-12-03Rollup merge of #133681 - RalfJung:niches, r=wesleywiserMatthias Krüger-0/+2
improve TagEncoding::Niche docs, sanity check, and UB checks Turns out the `niche_variants` range can actually contain the `untagged_variant`. We should report this as UB in Miri, so this PR implements that. Also rename `partially_check_layout` to `layout_sanity_check` for better consistency with how similar functions are called in other parts of the compiler. Turns out my adjustments to the transmutation logic also fix https://github.com/rust-lang/rust/issues/126267.
2024-12-03Bounds-check with PtrMetadata instead of Len in MIRScott McMurray-0/+3
2024-12-03Auto merge of #133321 - compiler-errors:const-checker, r=wesleywiserbors-5/+7
Get rid of HIR const checker As far as I can tell, the HIR const checker was implemented in https://github.com/rust-lang/rust/pull/66170 because we were not able to issue useful const error messages in the MIR const checker. This seems to have changed in the last 5 years, probably due to work like #90532. I've tweaked the diagnostics slightly and think the error messages have gotten *better* in fact. Thus I think the HIR const checker has reached the end of its usefulness, and we can retire it. cc `@RalfJung`
2024-12-02Rollup merge of #133704 - RalfJung:promoted-size-overflow-ice, r=compiler-errorsGuillaume Gomez-17/+9
fix ICE when promoted has layout size overflow Turns out there is no reason to distinguish `tainted_by_errors` and `can_be_spurious` here, we can just track whether we allow this even in "infallible" constants. Fixes https://github.com/rust-lang/rust/issues/125476
2024-12-02Rollup merge of #133610 - camelid:move-from_anon_const, r=BoxyUwUGuillaume Gomez-82/+2
Move `Const::{from_anon_const,try_from_lit}` to hir_ty_lowering Fixes #128176. This accomplishes one of the followup items from #131081. These operations are much more about lowering the HIR than about `Const`s themselves. They fit better in hir_ty_lowering with `lower_const_arg` (formerly `Const::from_const_arg`) and the rest. To accomplish this, `const_evaluatable_predicates_of` had to be changed to not use `from_anon_const` anymore. Instead of visiting the HIR and lowering anon consts on the fly, it now visits the `rustc_middle::ty` data structures instead and directly looks for `UnevaluatedConst`s. This approach was proposed in: https://github.com/rust-lang/rust/pull/131081#discussion_r1821189257 r? `@BoxyUwU`
2024-12-02Move `Const::{from_anon_const,try_from_lit}` to hir_ty_loweringNoah Lev-82/+2
These operations are much more about lowering the HIR than about `Const`s themselves. They fit better in hir_ty_lowering with `lower_const_arg` (formerly `Const::from_const_arg`) and the rest. To accomplish this, `const_evaluatable_predicates_of` had to be changed to not use `from_anon_const` anymore. Instead of visiting the HIR and lowering anon consts on the fly, it now visits the `rustc_middle::ty` data structures instead and directly looks for `UnevaluatedConst`s. This approach was proposed in: https://github.com/rust-lang/rust/pull/131081#discussion_r1821189257
2024-12-02Rollup merge of #133751 - lcnr:no-trait-solving-on-type, r=compiler-errorsGuillaume Gomez-15/+18
remove `Ty::is_copy_modulo_regions` Using these functions is likely incorrect if an `InferCtxt` is available, I moved this function to `TyCtxt` (and added it to `LateContext`) and added a note to the documentation that one should prefer `Infer::type_is_copy_modulo_regions` instead. I didn't yet move `is_sized` and `is_freeze`, though I think we should move these as well. r? `@compiler-errors` cc #132279
2024-12-02remove `Ty::is_copy_modulo_regions`lcnr-15/+18
2024-12-02Auto merge of #133728 - jhpratt:rollup-k1i60pg, r=jhprattbors-2/+0
Rollup of 4 pull requests Successful merges: - #133589 (Remove `hir::ArrayLen`) - #133672 (Remove a bunch of unnecessary const stability noise) - #133678 (Stabilize `ptr::fn_addr_eq`) - #133727 (Update mailmap) r? `@ghost` `@rustbot` modify labels: rollup
2024-12-01Rollup merge of #133589 - voidc:remove-array-len, r=boxyuwuJacob Pratt-2/+0
Remove `hir::ArrayLen` This refactoring removes `hir::ArrayLen`, replacing it with `hir::ConstArg`. To represent inferred array lengths (previously `hir::ArrayLen::Infer`), a new variant `ConstArgKind::Infer` is added. r? `@BoxyUwU`
2024-12-01Rollup merge of #133446 - Zalathar:querify, r=cjgillotJacob Pratt-5/+13
coverage: Use a query to identify which counter/expression IDs are used Given that we already have a query to identify the highest-numbered counter ID in a MIR body, we can extend that query to also build bitsets of used counter/expression IDs. That lets us avoid some messy coverage bookkeeping during the main MIR traversal for codegen. This does mean that we fail to treat some IDs as used in certain MIR-inlining scenarios, but I think that's fine, because it means that the results will be consistent across all instantiations of a function. --- There's some more cleanup I want to do in the function coverage collector, since it isn't really collecting anything any more, but I'll leave that for future work.
2024-12-01fix ICE when promoted has layout size overflowRalf Jung-17/+9
2024-12-02rust_for_linux: -Zreg-struct-return commandline flag for X86 (#116973)Andrew Zhogin-1/+4
2024-12-01fix safe-transmute handling of enumsRalf Jung-0/+2
2024-12-01Adjust HostEffect error spans correctly to point at argsMichael Goulet-0/+4
2024-11-30Remove hir::ArrayLen, introduce ConstArgKind::InferDominik Stolz-2/+0
Remove Node::ArrayLenInfer
2024-11-30Make compare_impl_item into a queryMichael Goulet-4/+7
2024-11-30coverage: Use a query to identify which counter/expression IDs are usedZalathar-3/+13
2024-11-30coverage: Allow niches in counter/expression IDsZalathar-2/+0
There is unlikely to be any practical difference between a counter limit of 2^32 and a counter limit of (2^32 - 256).