Commit Graph

1987 Commits

Author SHA1 Message Date
Maybe Waffle
236352024b make StackPop field names less confusing 2024-07-07 18:16:38 +02:00
Maybe Waffle
cda6f0c25d doc fixups from review 2024-07-07 18:16:38 +02:00
Maybe Waffle
6d4995f4e6 add miri tests and a fixme 2024-07-07 18:16:38 +02:00
Maybe Waffle
cda25e56c8 Refactor & fixup interpreter implementation of tail calls 2024-07-07 18:16:38 +02:00
Maybe Waffle
45c70318f7 Refactor common part of evaluating Call&TailCall in the interpreter 2024-07-07 17:11:05 +02:00
Maybe Waffle
3b5a5ee6c8 Support tail calls in the interpreter 2024-07-07 17:11:05 +02:00
Maybe Waffle
484152d562 Support tail calls in mir via TerminatorKind::TailCall 2024-07-07 17:11:04 +02:00
Ralf Jung
f6c377c350 offset_from intrinsic: always allow pointers to point to the same address 2024-07-06 17:14:26 +02:00
Matthias Krüger
2137d19ef6
Rollup merge of #127275 - RalfJung:offset-from-isize-min, r=Amanieu
offset_from, offset: clearly separate safety requirements the user needs to prove from corollaries that automatically follow

By landing https://github.com/rust-lang/rust/pull/116675 we decided that objects larger than `isize::MAX` cannot exist in the address space of a Rust program, which lets us simplify these rules.

For `offset_from`, we can even state that the *absolute* distance fits into an `isize`, and therefore exclude `isize::MIN`. This PR also changes Miri to treat an `isize::MIN` difference like the other isize-overflowing cases.
2024-07-06 13:26:25 +02:00
bors
489233170a Auto merge of #123781 - RalfJung:miri-fn-identity, r=oli-obk
Miri function identity hack: account for possible inlining

Having a non-lifetime generic is not the only reason a function can be duplicated. Another possibility is that the function may be eligible for cross-crate inlining. So also take into account the inlining attribute in this Miri hack for function pointer identity.

That said, `cross_crate_inlinable` will still sometimes return true even for `inline(never)` functions:
- when they are `DefKind::Ctor(..) | DefKind::Closure` -- I assume those cannot be `InlineAttr::Never` anyway?
- when `cross_crate_inline_threshold == InliningThreshold::Always`

so maybe this is still not quite the right criterion to use for function pointer identity.
2024-07-04 23:45:56 +00:00
Ralf Jung
273d253ce6 offset_from: "the difference must fit in an isize" is a corollary
also, isize::MIN is an impossible distance
2024-07-04 14:12:23 +02:00
bors
c872a1418a Auto merge of #125507 - compiler-errors:type-length-limit, r=lcnr
Re-implement a type-size based limit

r? lcnr

This PR reintroduces the type length limit added in #37789, which was accidentally made practically useless by the caching changes to `Ty::walk` in #72412, which caused the `walk` function to no longer walk over identical elements.

Hitting this length limit is not fatal unless we are in codegen -- so it shouldn't affect passes like the mir inliner which creates potentially very large types (which we observed, for example, when the new trait solver compiles `itertools` in `--release` mode).

This also increases the type length limit from `1048576 == 2 ** 20` to `2 ** 24`, which covers all of the code that can be reached with craterbot-check. Individual crates can increase the length limit further if desired.

Perf regression is mild and I think we should accept it -- reinstating this limit is important for the new trait solver and to make sure we don't accidentally hit more type-size related regressions in the future.

Fixes #125460
2024-07-03 11:56:36 +00:00
Michael Goulet
b1059ccda2 Instance::resolve -> Instance::try_resolve, and other nits 2024-07-02 17:28:03 -04:00
Michael Goulet
3273ccea4b Fix spans 2024-07-02 15:48:48 -04:00
Michael Goulet
9dc129ae82 Give Instance::expect_resolve a span 2024-07-02 15:48:48 -04:00
Michael Goulet
d3a742bde9 Miscellaneous renaming 2024-07-02 15:48:48 -04:00
Ralf Jung
41b98da42d Miri function identity hack: account for possible inlining 2024-07-02 21:05:30 +02:00
hattizai
ada9fda7c3 chore: remove duplicate words 2024-07-02 11:25:31 +08:00
bors
ba1d7f4a08 Auto merge of #120639 - fee1-dead-contrib:new-effects-desugaring, r=oli-obk
Implement new effects desugaring

cc `@rust-lang/project-const-traits.` Will write down notes once I have finished.

* [x] See if we want `T: Tr` to desugar into `T: Tr, T::Effects: Compat<true>`
* [x] Fix ICEs on `type Assoc: ~const Tr` and `type Assoc<T: ~const Tr>`
* [ ] add types and traits to minicore test
* [ ] update rustc-dev-guide

Fixes #119717
Fixes #123664
Fixes #124857
Fixes #126148
2024-06-29 20:08:10 +00:00
Deadbeef
72e8244e64 implement new effects desugaring 2024-06-28 10:57:35 +00:00
Trevor Gross
648cb16920 Enable const casting for f16 and f128 2024-06-27 04:36:29 -05:00
Ralf Jung
763e3131cc don't ICE when encountering an extern type field during validation 2024-06-22 17:39:01 +02:00
Jubilee
1916b3d57f
Rollup merge of #126811 - compiler-errors:tidy-ftl, r=estebank
Add a tidy rule to check that fluent messages and attrs don't end in `.`

This adds a new dependency on `fluent-parse` to `tidy` -- we already rely on it in rustc so I feel like it's not that big of a deal.

This PR also adjusts many error messages that currently end in `.`; not all of them since I added an `ALLOWLIST`, excluded `rustc_codegen_*` ftl files, and `.teach_note` attributes.

r? ``@estebank`` ``@oli-obk``
2024-06-21 21:02:29 -07:00
Jubilee
9498d5cf2f
Rollup merge of #126787 - Strophox:get-bytes, r=RalfJung
Add direct accessors for memory addresses in `Machine` (for Miri)

The purpose of this PR is to enable direct (immutable) access to memory addresses in `Machine`, which will be needed for further extension of Miri.

This is done by adding (/completing missings pairs of) accessor functions, with the relevant signatures as follows:
```rust
/* rust/compiler/rustc_middle/src/mir/interpret/allocation.rs */

pub trait AllocBytes {
  // ..

  fn as_ptr(&self) -> *const u8;
/*fn as_mut_ptr(&mut self) -> *mut u8; -- Already in the compiler*/
}

impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
  // ..

  pub fn get_bytes_unchecked_raw(&self) -> *const u8;
/*pub fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8; -- Already in the compiler*/
}
```
```rust
/* rust/compiler/rustc_const_eval/src/interpret/memory.rs */

impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
  // ..

  pub fn get_alloc_bytes_unchecked_raw(&self, id: AllocId) -> InterpResult<'tcx, *const u8>;
  pub fn get_alloc_bytes_unchecked_raw_mut(&mut self, id: AllocId) -> InterpResult<'tcx, *mut u8>;
}
```

r? ``@RalfJung``
2024-06-21 21:02:27 -07:00
Michael Goulet
ffd72b1700 Fix remaining cases 2024-06-21 19:00:18 -04:00
bors
5ced3dad57 Auto merge of #125853 - tesuji:promote-fail-fast, r=cjgillot
promote_consts: some clean-up after experimenting

This is some clean-up after experimenting in #125916,
Prefer to review commit-by-commit.
2024-06-21 16:00:14 +00:00
Lzu Tao
7002a3f37f interpret: use trace to reduce noice 2024-06-21 13:51:33 +00:00
Strophox
b512bf6f77 add as_ptr to trait AllocBytes, fix 2 impls; add pub fn get_bytes_unchecked_raw in allocation.rs; add pub fn get_alloc_bytes_unchecked_raw[_mut] in memory.rs 2024-06-21 12:50:24 +02:00
Scott McMurray
4a7b6c0e6c More GVN for PtrMetadata
`PtrMetadata` doesn't care about `*const`/`*mut`/`&`/`&mut`, so GVN away those casts in its argument.

This includes updating MIR to allow calling PtrMetadata on references too, not just raw pointers.  That means that `[T]::len` can be just `_0 = PtrMetadata(_1)`, for example.

# Conflicts:
#	tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir
#	tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir
2024-06-20 22:16:59 -07:00
Scott McMurray
e04e35133f bug! more uses of these in runtime stuff 2024-06-19 10:44:01 -07:00
León Orell Valerian Liehr
035285b464
Rollup merge of #126154 - RalfJung:storage-live, r=compiler-errors
StorageLive: refresh storage (instead of UB) when local is already live

Blocked on [this FCP](https://github.com/rust-lang/rust/issues/99160#issuecomment-2155924538), which also contains the motivation.

Fixes https://github.com/rust-lang/rust/issues/99160
Fixes https://github.com/rust-lang/rust/issues/98896 (by declaring it not-a-bug)
Fixes https://github.com/rust-lang/rust/issues/119366
Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/129
2024-06-19 13:04:58 +02:00
许杰友 Jieyou Xu (Joe)
0e46111660
Rollup merge of #126493 - jswrenn:fix-126460, r=compiler-errors
safe transmute: support non-ZST, variantful, uninhabited enums

Previously, `Tree::from_enum`'s implementation branched into three disjoint cases:

 1. enums that uninhabited
 2. enums for which all but one variant is uninhabited
 3. enums with multiple variants

This branching (incorrectly) did not differentiate between variantful and variantless uninhabited enums. In both cases, we assumed (and asserted) that uninhabited enums are zero-sized types. This assumption is false for enums like:

    enum Uninhabited { A(!, u128) }

...which, currently, has the same size as `u128`. This faulty assumption manifested as the ICE reported in #126460.

In this PR, we revise the first case of `Tree::from_enum` to consider only the narrow category of "enums that are uninhabited ZSTs". These enums, whose layouts are described with `Variants::Single { index }`, are special in their layouts otherwise resemble the `!` type and cannot be descended into like typical enums. This first case captures uninhabited enums like:

    enum Uninhabited { A(!, !), B(!) }

The second case is revised to consider the broader category of "enums that defer their layout to one of their variants"; i.e., enums whose layouts are described with `Variants::Single { index }` and that do have a variant at `index`. This second case captures uninhabited enums that are not ZSTs, like:

    enum Uninhabited { A(!, u128) }

...which represent their variants with `Variants::Single`.

Finally, the third case is revised to cover the broader category of "enums with multiple variants", which captures uninhabited enums like:

    enum Uninhabited { A(u8, !), B(!, u32) }

...which represent their variants with `Variants::Multiple`.

This PR also adds a comment requested by ````@RalfJung```` in his review of #126358 to `compiler/rustc_const_eval/src/interpret/discriminant.rs`.

Fixes #126460

r? ````@compiler-errors````
2024-06-19 01:51:39 +01:00
Oli Scherer
3f34196839 Remove redundant argument from subdiagnostic method 2024-06-18 15:42:11 +00:00
Oli Scherer
7ba82d61eb Use a dedicated type instead of a reference for the diagnostic context
This paves the way for tracking more state (e.g. error tainting) in the diagnostic context handle
2024-06-18 15:42:11 +00:00
Matthias Krüger
940ff24ec0
Rollup merge of #126567 - compiler-errors:instance-kind, r=oli-obk,lcnr
Rename `InstanceDef` -> `InstanceKind`

Renames `InstanceDef` to `InstanceKind`. The `Def` here is confusing, and makes it hard to distinguish `Instance` and `InstanceDef`. `InstanceKind` makes this more obvious, since it's really just describing what *kind* of instance we have.

Not sure if this is large enough to warrant a types team MCP -- it's only 53 files. I don't personally think it does, but happy to write one if anyone disagrees. cc ``@rust-lang/types``

r? types
2024-06-17 20:34:51 +02:00
Oli Scherer
4e5dfb61e4 Remove an unused validation error variant 2024-06-17 10:55:42 +00:00
Michael Goulet
342c1b03d6 Rename InstanceDef -> InstanceKind 2024-06-16 21:35:21 -04:00
bors
92af831290 Auto merge of #126518 - matthiaskrgr:rollup-wb70rzq, r=matthiaskrgr
Rollup of 9 pull requests

Successful merges:

 - #125829 (rustc_span: Add conveniences for working with span formats)
 - #126361 (Unify intrinsics body handling in StableMIR)
 - #126417 (Add `f16` and `f128` inline ASM support for `x86` and `x86-64`)
 - #126424 ( Also sort `crt-static` in `--print target-features` output)
 - #126428 (Polish `std::path::absolute` documentation.)
 - #126429 (Add `f16` and `f128` const eval for binary and unary operationations)
 - #126448 (End support for Python 3.8 in tidy)
 - #126488 (Use `std::path::absolute` in bootstrap)
 - #126511 (.mailmap: Associate both my work and my private email with me)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-06-15 14:51:12 +00:00
Matthias Krüger
3775f2f5d0
Rollup merge of #126429 - tgross35:f16-f128-const-eval, r=RalfJung
Add `f16` and `f128` const eval for binary and unary operationations

Add const evaluation and Miri support for f16 and f128, including unary and binary operations. Casts are not yet included.

Fixes https://github.com/rust-lang/rust/issues/124583

r? ``@RalfJung``
2024-06-15 14:40:50 +02:00
Matthias Krüger
f24509aa23
Rollup merge of #126469 - RalfJung:mir-shifts, r=scottmcm
MIR Shl/Shr: the offset can be computed with rem_euclid

r? ````@scottmcm````
2024-06-15 10:56:41 +02:00
Jack Wrenn
df1d6168f4 safe transmute: support non-ZST, variantful, uninhabited enums
Previously, `Tree::from_enum`'s implementation branched into three disjoint
cases:

 1. enums that uninhabited
 2. enums for which all but one variant is uninhabited
 3. enums with multiple inhabited variants

This branching (incorrectly) did not differentiate between variantful and
variantless uninhabited enums. In both cases, we assumed (and asserted) that
uninhabited enums are zero-sized types. This assumption is false for enums like:

    enum Uninhabited { A(!, u128) }

...which, currently, has the same size as `u128`. This faulty assumption
manifested as the ICE reported in #126460.

In this PR, we revise the first case of `Tree::from_enum` to consider only the
narrow category of "enums that are uninhabited ZSTs". These enums, whose layouts
are described with `Variants::Single { index }`, are special in their layouts
otherwise resemble the `!` type and cannot be descended into like typical enums.
This first case captures uninhabited enums like:

    enum Uninhabited { A(!, !), B(!) }

The second case is revised to consider the broader category of "enums that defer
their layout to one of their variants"; i.e., enums whose layouts are described
with `Variants::Single { index }` and that do have a variant at `index`. This
second case captures uninhabited enums that are not ZSTs, like:

    enum Uninhabited { A(!, u128) }

...which represent their variants with `Variants::Single`.

Finally, the third case is revised to cover the broader category of "enums with
multiple variants", which captures uninhabited, non-ZST enums like:

    enum Uninhabited { A(u8, !), B(!, u32) }

...which represent their variants with `Variants::Multiple`.

This PR also adds a comment requested by RalfJung in his review of #126358 to
`compiler/rustc_const_eval/src/interpret/discriminant.rs`.

Fixes #126460
2024-06-14 21:11:08 +00:00
Michael Goulet
93ff86ed7c Use is_lang_item more aggressively 2024-06-14 16:54:29 -04:00
Trevor Gross
eab5e8e9d9 Make the unary operator FloatTy check exhaustive
Add a spot that was missed in
<https://github.com/rust-lang/rust/pull/121997>.
2024-06-14 12:43:58 -05:00
Trevor Gross
c906d2e428 Enable const evaluation for f16 and f128
This excludes casting, which needs more tests.
2024-06-14 12:43:48 -05:00
Matthias Krüger
aebd794d15
Rollup merge of #126426 - RalfJung:dangling-zst-ice, r=oli-obk
const validation: fix ICE on dangling ZST reference

Fixes https://github.com/rust-lang/rust/issues/126393
I'm not super happy with this fix but I can't think of a better one.

r? `@oli-obk`
2024-06-14 12:23:38 +02:00
Ralf Jung
669a40006b MIR Shl/Shr: the offset can be computed with rem_euclid 2024-06-14 11:52:00 +02:00
Ralf Jung
a6907100de const validation: fix ICE on dangling ZST reference 2024-06-14 07:52:51 +02:00
Ralf Jung
54e24c1573 const-eval: make lint scope computation consistent 2024-06-13 20:31:00 +02:00
Ralf Jung
b316033dd8 rename CompileTimeInterpreter -> CompileTimeMachine, CompileTimeEvalContext -> CompileTimeInterpCx
to match the terms used in the shared interpreter infrastructure
2024-06-13 20:30:11 +02:00
León Orell Valerian Liehr
393b526018
Rollup merge of #126379 - RalfJung:find_closest_untracked_caller_location, r=oli-obk
interpret: update doc comment for find_closest_untracked_caller_location

Also add a doc comment to cur_span.

r? `@compiler-errors`
2024-06-13 13:05:27 +02:00
Ralf Jung
d87ec03ed9 interpret: update doc comment for find_closest_untracked_caller_location 2024-06-13 09:08:57 +02:00
Jubilee
d33ec8ed8c
Rollup merge of #126358 - jswrenn:fix-125811, r=compiler-errors
safe transmute: support `Single` enums

Previously, the implementation of `Tree::from_enum` incorrectly treated enums with `Variants::Single` and `Variants::Multiple` identically. This is incorrect for `Variants::Single` enums, which delegate their layout to that of a variant with a particular index (or no variant at all if the enum is empty).

This flaw manifested first as an ICE. `Tree::from_enum` attempted to compute the tag of variants other than the one at `Variants::Single`'s `index`, and fell afoul of a sanity-checking assertion in `compiler/rustc_const_eval/src/interpret/discriminant.rs`. This assertion is non-load-bearing, and can be removed; the routine its in is well-behaved even without it.

With the assertion removed, the proximate issue becomes apparent: calling `Tree::from_variant` on a variant that does not exist is ill-defined. A sanity check the given variant has `FieldShapes::Arbitrary` fails, and the analysis is (correctly) aborted with `Err::NotYetSupported`.

This commit corrects this chain of failures by ensuring that `Tree::from_variant` is not called on variants that are, as far as layout is concerned, nonexistent. Specifically, the implementation of `Tree::from_enum` is now partitioned into three cases:

  1. enums that are uninhabited
  2. enums for which all but one variant is uninhabited
  3. enums with multiple inhabited variants

`Tree::from_variant` is now only invoked in the third case. In the first case, `Tree::uninhabited()` is produced. In the second case, the layout is delegated to `Variants::Single`'s index.

Fixes #125811
2024-06-12 20:03:22 -07:00
Jubilee
f5af7eea1a
Rollup merge of #126328 - RalfJung:is_none_or, r=workingjubilee
Add Option::is_none_or

ACP: https://github.com/rust-lang/libs-team/issues/212
2024-06-12 20:03:20 -07:00
Jack Wrenn
fb662f2126 safe transmute: support Variants::Single enums
Previously, the implementation of `Tree::from_enum` incorrectly
treated enums with `Variants::Single` and `Variants::Multiple`
identically. This is incorrect for `Variants::Single` enums,
which delegate their layout to that of a variant with a particular
index (or no variant at all if the enum is empty).

This flaw manifested first as an ICE. `Tree::from_enum` attempted
to compute the tag of variants other than the one at
`Variants::Single`'s `index`, and fell afoul of a sanity-checking
assertion in `compiler/rustc_const_eval/src/interpret/discriminant.rs`.
This assertion is non-load-bearing, and can be removed; the routine
its in is well-behaved even without it.

With the assertion removed, the proximate issue becomes apparent:
calling `Tree::from_variant` on a variant that does not exist is
ill-defined. A sanity check the given variant has
`FieldShapes::Arbitrary` fails, and the analysis is (correctly)
aborted with `Err::NotYetSupported`.

This commit corrects this chain of failures by ensuring that
`Tree::from_variant` is not called on variants that are, as far as
layout is concerned, nonexistent. Specifically, the implementation
of `Tree::from_enum` is now partitioned into three cases:

  1. enums that are uninhabited
  2. enums for which all but one variant is uninhabited
  3. enums with multiple inhabited variants

`Tree::from_variant` is now only invoked in the third case. In the
first case, `Tree::uninhabited()` is produced. In the second case,
the layout is delegated to `Variants::Single`'s index.

Fixes #125811
2024-06-13 01:38:51 +00:00
Ralf Jung
4c208ac233 use is_none_or in some places in the compiler 2024-06-12 16:20:07 +02:00
Guillaume Gomez
51a58c59f3
Rollup merge of #126232 - RalfJung:dyn-trait-equality, r=oli-obk
interpret: dyn trait metadata check: equate traits in a proper way

Hopefully fixes https://github.com/rust-lang/miri/issues/3541... unfortunately we don't have a testcase.

The first commit is just a refactor without functional change.

r? `@oli-obk`
2024-06-12 15:44:59 +02:00
Nicholas Nethercote
75b164d836 Use tidy to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.

For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
  `allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
  sometimes the order is alphabetical, and sometimes there is no
  particular order.
- Sometimes the attributes of a particular kind aren't even grouped
  all together, e.g. there might be a `feature`, then an `allow`, then
  another `feature`.

This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.

Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
  because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
  ignored in `rustfmt.toml`).
2024-06-12 15:49:10 +10:00
Ralf Jung
db44cae343 interpret: ensure we check bool/char for validity when they are used in a cast 2024-06-11 12:16:09 +02:00
Ralf Jung
3757136d8e interpret: dyn trait metadata check: equate traits in a proper way 2024-06-11 08:54:49 +02:00
Ralf Jung
d041b7cf30 check for correct trait in size_and_align_of 2024-06-11 08:54:49 +02:00
Matthias Krüger
07bb7ca9fa
Rollup merge of #126184 - RalfJung:interpret-simd-nonpow2, r=oli-obk
interpret: do not ICE on padded non-pow2 SIMD vectors

Fixes https://github.com/rust-lang/miri/issues/3458

r? ``@oli-obk``
2024-06-10 21:12:25 +02:00
Ralf Jung
af4d6c74ef interpret: refactor dyn trait handling
We can check that the vtable is for the right trait very early, and then just pass the type around.
2024-06-10 17:28:52 +02:00
Ralf Jung
3c57ea0df7 ScalarInt: size mismatches are a bug, do not delay the panic 2024-06-10 13:43:16 +02:00
Ralf Jung
d5fb8257e7 interpret: do not ICE on padded non-pow2 SIMD vectors 2024-06-09 11:54:23 +02:00
Ralf Jung
eb584a23bf offset_of: allow (unstably) taking the offset of slice tail fields 2024-06-08 18:17:55 +02:00
Ralf Jung
9b05e154f3 StorageLive: refresh storage (instead of UB) when local is already live 2024-06-08 12:24:48 +02:00
Oli Scherer
cbee17d502 Revert "Create const block DefIds in typeck instead of ast lowering"
This reverts commit ddc5f9b6c1.
2024-06-07 08:33:58 +00:00
bors
2d28b6384e Auto merge of #124482 - spastorino:unsafe-extern-blocks, r=oli-obk
Unsafe extern blocks

This implements RFC 3484.

Tracking issue #123743 and RFC https://github.com/rust-lang/rfcs/pull/3484

This is better reviewed commit by commit.
2024-06-06 08:14:58 +00:00
bors
003a902792 Auto merge of #125958 - BoxyUwU:remove_const_ty, r=lcnr
Remove the `ty` field from type system `Const`s

Fixes #125556
Fixes #122908

Part of the work on `adt_const_params`/`generic_const_param_types`/`min_generic_const_exprs`/generally making the compiler nicer. cc rust-lang/project-const-generics#44

Please review commit-by-commit otherwise I wasted a lot of time not just squashing this into a giant mess (and also it'll be SO much nicer because theres a lot of fluff changes mixed in with other more careful changes if looking via File Changes

---

Why do this?
- The `ty` field keeps causing ICEs and weird behaviour due to it either being treated as "part of the const" or it being forgotten about leading to ICEs.
- As we move forward with `adt_const_params` and a potential `min_generic_const_exprs` it's going to become more complex to actually lower the correct `Ty<'tcx>`
- It muddles the idea behind how we check `Const` arguments have the correct type. By having the `ty` field it may seem like we ought to be relating it when we relate two types, or that its generally important information about the `Const`.
- Brings the compiler more in line with `a-mir-formality` as that also tracks the type of type system `Const`s via `ConstArgHasType` bounds in the env instead of on the `Const` itself.
- A lot of stuff is a lot nicer when you dont have to pass around the type of a const lol. Everywhere we construct `Const` is now significantly nicer 😅

See #125671's description for some more information about the `ty` field

---

General summary of changes in this PR:

- Add `Ty` to `ConstKind::Value` as otherwise there is no way to implement `ConstArgHasType` to ensure that const arguments are correctly typed for the parameter when we stop creating anon consts for all const args. It's also just incredibly difficult/annoying to thread the correct `Ty` around to a bunch of ctfe functions otherwise.
-  Fully implement `ConstArgHasType` in both the old and new solver. Since it now has no reliance on the `ty` field it serves its originally intended purpose of being able to act as a double check that trait vs impls have correctly typed const parameters. It also will now be able to be responsible for checking types of const arguments to parameters under `min_generic_const_exprs`.
- Add `Ty` to `mir::Const::Ty`. I dont have a great understanding of why mir constants are setup like this to be honest. Regardless they need to be able to determine the type of the const and the easiest way to make this happen was to simply store the `Ty` along side the `ty::Const`. Maybe we can do better here in the future but I'd have to spend way more time looking at everywhere we use `mir::Const`.
- rustdoc has its own `Const` which also has a `ty` field. It was relatively easy to remove this.

---

r? `@lcnr` `@compiler-errors`
2024-06-06 03:41:23 +00:00
Boxy
60a5bebbe5 Add Ty to mir::Const::Ty 2024-06-05 22:25:41 +01:00
Boxy
a9702a6668 Add Ty to ConstKind::Value 2024-06-05 22:25:41 +01:00
Ben Kimock
b710404f3b Update the interpreter to handle the new cases 2024-06-05 09:04:37 -04:00
Santiago Pastorino
bac72cf7cf
Add safe/unsafe to static inside extern blocks 2024-06-04 14:19:43 -03:00
Michael Goulet
eb0a70a557 Opt-in diagnostics reporting to avoid doing extra work in the new solver 2024-06-03 09:27:52 -04:00
bors
1d52972dd8 Auto merge of #125778 - estebank:issue-67100, r=compiler-errors
Use parenthetical notation for `Fn` traits

Always use the `Fn(T) -> R` format when printing closure traits instead of `Fn<(T,), Output = R>`.

Address #67100:

```
error[E0277]: expected a `Fn()` closure, found `F`
 --> file.rs:6:13
  |
6 |     call_fn(f)
  |     ------- ^ expected an `Fn()` closure, found `F`
  |     |
  |     required by a bound introduced by this call
  |
  = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `call_fn`
 --> file.rs:1:15
  |
1 | fn call_fn<F: Fn() -> ()>(f: &F) {
  |               ^^^^^^^^^^ required by this bound in `call_fn`
help: consider further restricting this bound
  |
5 | fn call_any<F: std::any::Any + Fn()>(f: &F) {
  |                              ++++++
```
2024-06-03 08:14:03 +00:00
Michael Goulet
333458c2cb Uplift TypeRelation and Relate 2024-06-01 12:50:58 -04:00
Esteban Küber
e6bd6c2044 Use parenthetical notation for Fn traits
Always use the `Fn(T) -> R` format when printing closure traits instead of `Fn<(T,), Output = R>`.

Fix #67100:

```
error[E0277]: expected a `Fn()` closure, found `F`
 --> file.rs:6:13
  |
6 |     call_fn(f)
  |     ------- ^ expected an `Fn()` closure, found `F`
  |     |
  |     required by a bound introduced by this call
  |
  = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `call_fn`
 --> file.rs:1:15
  |
1 | fn call_fn<F: Fn() -> ()>(f: &F) {
  |               ^^^^^^^^^^ required by this bound in `call_fn`
help: consider further restricting this bound
  |
5 | fn call_any<F: std::any::Any + Fn()>(f: &F) {
  |                              ++++++
```
2024-05-29 22:26:54 +00:00
许杰友 Jieyou Xu (Joe)
305137de18
Rollup merge of #125633 - RalfJung:miri-no-copy, r=saethlin
miri: avoid making a full copy of all new allocations

Hopefully fixes https://github.com/rust-lang/miri/issues/3637

r? ``@saethlin``
2024-05-29 03:25:09 +01:00
Scott McMurray
459ce3f6bb Add an intrinsic for ptr::metadata 2024-05-28 09:28:51 -07:00
Oli Scherer
ddc5f9b6c1 Create const block DefIds in typeck instead of ast lowering 2024-05-28 13:38:43 +00:00
Oli Scherer
be94ca0bcd Remove a CTFE check that was only ever used to ICE
The guarded call will ICE on its own.

While this improved diagnostics in the presence of bugs somewhat, it is also a blocker to query feeding of constants. If this case is hit again, we should instead improve diagnostics of the root ICE
2024-05-28 11:36:30 +00:00
Ralf Jung
869306418d miri: avoid making a full copy of all new allocations 2024-05-27 23:33:54 +02:00
Ralf Jung
e8379c9598 interpret: get rid of 'mir lifetime everywhere 2024-05-27 08:25:57 +02:00
Ralf Jung
36d36a3e1f interpret: the MIR is actually at lifetime 'tcx 2024-05-27 07:45:41 +02:00
Peter Jaszkowiak
4913ab8f77
Stabilize LazyCell and LazyLock (lazy_cell) 2024-02-20 20:55:13 -07:00
Matthias Krüger
f23ebf0410
Rollup merge of #125483 - workingjubilee:move-transform-validate-to-mir-transform, r=oli-obk
compiler: validate.rs belongs next to what it validates

It's hard to find code that is deeply nested and far away from its callsites, so let's move `rustc_const_eval::transform::validate` into `rustc_mir_transform`, where all of its callers are. As `rustc_mir_transform` already depends on `rustc_const_eval`, the added visible dependency edge doesn't mean the dependency tree got any worse.

This also lets us unnest the `check_consts` module.

I did look into moving everything inside `rustc_const_eval::transform` into `rustc_mir_transform`. It turned out to be a much more complex operation, with more concerns and real edges into the `const_eval` crate, whereas this was both faster and more obvious.
2024-05-24 23:01:09 +02:00
Jubilee Young
87048a46fc compiler: unnest rustc_const_eval::check_consts 2024-05-24 09:56:56 -07:00
Jubilee Young
db6ec2618a compiler: const_eval/transform/validate.rs -> mir_transform/validate.rs 2024-05-24 09:56:56 -07:00
Matthias Krüger
eb6297eb6f
Rollup merge of #125477 - nnethercote:missed-rustfmt, r=compiler-errors
Run rustfmt on files that need it.

Somehow these files aren't properly formatted. By default `x fmt` and `x tidy` only check files that have changed against master, so if an ill-formatted file somehow slips in it can stay that way as long as it doesn't get modified(?)

I found these when I ran `x fmt` explicitly on every `.rs` file in the repo, while working on
https://github.com/rust-lang/compiler-team/issues/750.
2024-05-24 17:48:03 +02:00
bors
464987730a Auto merge of #125479 - scottmcm:validate-vtable-projections, r=Nilstrieb
Validate the special layout restriction on `DynMetadata`

If you look at <https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/std/ptr/struct.DynMetadata.html>, you'd think that `DynMetadata` is a struct with fields.

But it's actually not, because the lang item is special-cased in rustc_middle layout:

7601adcc76/compiler/rustc_middle/src/ty/layout.rs (L861-L864)

That explains the very confusing codegen ICEs I was getting in https://github.com/rust-lang/rust/pull/124251#issuecomment-2128543265

> Tried to extract_field 0 from primitive OperandRef(Immediate((ptr:  %5 = load ptr, ptr %4, align 8, !nonnull !3, !align !5, !noundef !3)) @ TyAndLayout { ty: DynMetadata<dyn Callsite>, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) } })

because there was a `Field` projection despite the layout clearly saying it's [`Primitive`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/abi/enum.FieldsShape.html#variant.Primitive).

Thus this PR updates the MIR validator to check for such a projection, and changes `libcore` to not ever emit any projections into `DynMetadata`, just to transmute the whole thing when it wants a pointer.
2024-05-24 08:53:27 +00:00
Scott McMurray
d83f3ca8ca Validate the special layout restriction on DynMetadata 2024-05-23 23:38:44 -07:00
Nicholas Nethercote
c1ac4a2f28 Run rustfmt on files that need it.
Somehow these files aren't properly formatted. By default `x fmt` and `x
tidy` only check files that have changed against master, so if an
ill-formatted file somehow slips in it can stay that way as long as it
doesn't get modified(?)

I found these when I ran `x fmt` explicitly on every `.rs` file in the
repo, while working on
https://github.com/rust-lang/compiler-team/issues/750.
2024-05-24 15:17:21 +10:00
bors
8679004993 Auto merge of #125434 - nnethercote:rm-more-extern-tracing, r=jackh726
Remove more `#[macro_use] extern crate tracing`

Because explicit importing of macros via use items is nicer (more standard and readable) than implicit importing via `#[macro_use]`. Continuing the work from #124511 and #124914.

r? `@jackh726`
2024-05-23 21:36:54 +00:00
Oli Scherer
4cf34cb752 Allow const eval failures if the cause is a type layout issue 2024-05-23 10:51:52 +00:00
Nicholas Nethercote
7a5d814a04 Remove #[macro_use] extern crate tracing from rustc_const_eval. 2024-05-23 18:02:38 +10:00
bors
5293c6adb7 Auto merge of #125359 - RalfJung:interpret-overflowing-ops, r=oli-obk
interpret: make overflowing binops just normal binops

Follow-up to https://github.com/rust-lang/rust/pull/125173 (Cc `@scottmcm)`
2024-05-23 04:03:14 +00:00
bors
5d328a1f62 Auto merge of #117329 - RalfJung:offset-by-zero, r=oli-obk,scottmcm
offset: allow zero-byte offset on arbitrary pointers

As per prior `@rust-lang/opsem` [discussion](https://github.com/rust-lang/opsem-team/issues/10) and [FCP](https://github.com/rust-lang/unsafe-code-guidelines/issues/472#issuecomment-1793409130):

- Zero-sized reads and writes are allowed on all sufficiently aligned pointers, including the null pointer
- Inbounds-offset-by-zero is allowed on all pointers, including the null pointer
- `offset_from` on two pointers derived from the same allocation is always allowed when they have the same address

This removes surprising UB (in particular, even C++ allows "nullptr + 0", which we currently disallow), and it brings us one step closer to an important theoretical property for our semantics ("provenance monotonicity": if operations are valid on bytes without provenance, then adding provenance can't make them invalid).

The minimum LLVM we require (v17) includes https://reviews.llvm.org/D154051, so we can finally implement this.

The `offset_from` change is needed to maintain the equivalence with `offset`: if `let ptr2 = ptr1.offset(N)` is well-defined, then `ptr2.offset_from(ptr1)` should be well-defined and return N. Now consider the case where N is 0 and `ptr1` dangles: we want to still allow offset_from here.

I think we should change offset_from further, but that's a separate discussion.

Fixes https://github.com/rust-lang/rust/issues/65108
[Tracking issue](https://github.com/rust-lang/rust/issues/117945) | [T-lang summary](https://github.com/rust-lang/rust/pull/117329#issuecomment-1951981106)

Cc `@nikic`
2024-05-22 13:04:14 +00:00
Ralf Jung
cb5319483e clarify comment
Co-authored-by: scottmcm <scottmcm@users.noreply.github.com>
2024-05-22 11:19:04 +02:00
Ralf Jung
9526ce60fd improve comment wording 2024-05-21 21:13:20 +02:00
Ralf Jung
c0b4b454c3 interpret: make overflowing binops just normal binops 2024-05-21 14:50:09 +02:00