Commit graph

258 commits

Author SHA1 Message Date
Ralf Jung b92a60586a rename LocalState::Uninitialized to Unallocated 2022-03-21 22:48:11 -04:00
bors 9bd53718e2 Auto merge of #95071 - RalfJung:arbitrary-self-dyn, r=oli-obk
Miri: implement arbitrary-self dyn receivers

Roughly follows the [codegen logic](851fcc7a54/compiler/rustc_codegen_ssa/src/mir/block.rs (L809)).

Fixes https://github.com/rust-lang/miri/issues/1038
r? `@oli-obk` Cc `@eddyb`
2022-03-20 13:48:23 +00:00
Ralf Jung fa5fa72fe1 Miri: implement arbitrary-self dyn receivers 2022-03-18 08:50:30 -04:00
mark bb8d4307eb rustc_error: make ErrorReported impossible to construct
There are a few places were we have to construct it, though, and a few
places that are more invasive to change. To do this, we create a
constructor with a long obvious name.
2022-03-16 10:35:24 -05:00
bors 012720ffb0 Auto merge of #94733 - nnethercote:fix-AdtDef-interning, r=fee1-dead
Improve `AdtDef` interning.

This commit makes `AdtDef` use `Interned`. Much of the commit is tedious
changes to introduce getter functions. The interesting changes are in
`compiler/rustc_middle/src/ty/adt.rs`.

r? `@fee1-dead`
2022-03-12 07:02:05 +00:00
Dylan DPC 9e70b1a033
Rollup merge of #94827 - RalfJung:offset-from-ub, r=oli-obk
CTFE/Miri: detect out-of-bounds pointers in offset_from

Also I became uneasy with aggressively doing `try_to_int` here -- this will always succeed on Miri, leading to the wrong codepath being taken. We should rather try to convert them both to pointers, and use the integer path as a fallback, so that's what I implemented now.

Hiding whitespaces helps with the diff.

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

r? ``@oli-obk``
2022-03-11 20:29:45 +01:00
Nicholas Nethercote ca5525d564 Improve AdtDef interning.
This commit makes `AdtDef` use `Interned`. Much the commit is tedious
changes to introduce getter functions. The interesting changes are in
`compiler/rustc_middle/src/ty/adt.rs`.
2022-03-11 13:31:24 +11:00
Ralf Jung 63ed8e41ce adjust offset_from logic: check that both pointers are in-bounds 2022-03-10 18:59:58 -05:00
b-naber 26fe550670 normalization change and rebase 2022-03-09 11:33:11 +01:00
Eric Holk 8700b45b67 Stabilize const_impl_trait as well 2022-03-07 08:47:18 -08:00
Eric Holk 801be21d11 Remove dead/useless code 2022-03-07 08:47:18 -08:00
Eric Holk 8fc835831c Update tests after feature stabilization 2022-03-07 08:47:18 -08:00
Eric Holk 7723506d13 Stabilize const_fn_fn_ptr_basics and const_fn_trait_bound 2022-03-07 08:47:15 -08:00
Matthias Krüger a795f0f536
Rollup merge of #94685 - RalfJung:saturating, r=oli-obk
interpret: move saturating_add/sub into (pub) helper method

I plan to use them for `simd_saturating_add/sub`.

The first commit just moves code, the 2nd simplifies it a bit with some helper methods that did not exist yet when the code was originally written.
2022-03-07 11:35:57 +01:00
Matthias Krüger f7eb3830df
Rollup merge of #94681 - RalfJung:miri-cast, r=oli-obk
CTFE engine: expose misc_cast to Miri

We need that to implement `simd_cast`/`simd_as` in Miri.

While at it, also change other code outside `cast.rs` to use `misc_cast` instead of lower-level methods.

r? `@oli-obk`
2022-03-07 06:44:04 +01:00
Ralf Jung ac844986d8 use singed_int_max/min helper methods 2022-03-06 19:11:31 -05:00
Ralf Jung 956659e5ce move saturating_add/sub into (pub) helper method 2022-03-06 19:09:22 -05:00
bors 8876ca3dd4 Auto merge of #94597 - nnethercote:ConstAllocation, r=fee1-dead
Introduce `ConstAllocation`.

Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.

This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.

In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.

The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.

r? `@fee1-dead`
2022-03-06 22:37:54 +00:00
Nicholas Nethercote 4852291417 Introduce ConstAllocation.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.

This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.

In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.

The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-07 08:25:50 +11:00
Ralf Jung d29d2307b9 CTFE engine: expose misc_cast to Miri 2022-03-06 14:01:01 -05:00
Ralf Jung dfc43df937 explain why shift with signed offset works the way it does 2022-03-06 11:29:24 -05:00
Dylan DPC 72c0c08445
Rollup merge of #94575 - RalfJung:switchint-comment, r=oli-obk
CTFE SwitchInt: update comment

I just wondered why this doesn't use `binary_op`.

r? ```@oli-obk```
2022-03-04 02:06:43 +01:00
Ralf Jung 0854781861 CTFE SwitchInt: update comment 2022-03-03 14:46:29 -05:00
Matthias Krüger 939c1585a4
Rollup merge of #94555 - cuishuang:master, r=oli-obk
all: fix some typos

Signed-off-by: cuishuang <imcusg@gmail.com>
2022-03-03 20:01:48 +01:00
bors 4566094913 Auto merge of #94512 - RalfJung:sdiv-ub, r=oli-obk
Miri/CTFE: properly treat overflow in (signed) division/rem as UB

To my surprise, it looks like LLVM treats overflow of signed div/rem as UB. From what I can tell, MIR `Div`/`Rem` directly lowers to the corresponding LLVM operation, so to make that correct we also have to consider these overflows UB in the CTFE/Miri interpreter engine.

r? `@oli-obk`
2022-03-03 12:56:24 +00:00
cuishuang 00fffdddd2 all: fix some typos
Signed-off-by: cuishuang <imcusg@gmail.com>
2022-03-03 19:47:23 +08:00
mark e489a94dee rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
Ralf Jung 6739299d18 Miri/CTFE: properly treat overflow in (signed) division/rem as UB 2022-03-01 20:39:51 -05:00
bors d981633ed6 Auto merge of #94290 - Mark-Simulacrum:bump-bootstrap, r=pietroalbini
Bump bootstrap to 1.60

This bumps the bootstrap compiler to 1.60 and cleans up cfgs and Span's rustc_pass_by_value (enabled by the bootstrap bump).
2022-02-25 18:34:02 +00:00
Matthias Krüger cf3bb09888
Rollup merge of #94343 - RalfJung:fn-ptr, r=oli-obk
Miri fn ptr check: don't use conservative null check

In https://github.com/rust-lang/rust/pull/94270 I used the wrong NULL check for function pointers: `memory.ptr_may_be_null` is conservative even on machines that support ptr-to-int casts, leading to false errors in Miri.

This fixes that problem, and also replaces that foot-fun of a method with `scalar_may_be_null` which is never unnecessarily conservative.

r? `@oli-obk`
2022-02-25 14:14:39 +01:00
Mark Rousskov 22c3a71de1 Switch bootstrap cfgs 2022-02-25 08:00:52 -05:00
Ralf Jung d8064d7d49 Miri fn ptr check: don't use conservative null check 2022-02-24 19:52:54 -05:00
bors d4de1f230c Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank
rustc_errors: let `DiagnosticBuilder::emit` return a "guarantee of emission".

That is, `DiagnosticBuilder` is now generic over the return type of `.emit()`, so we'll now have:
* `DiagnosticBuilder<ErrorReported>` for error (incl. fatal/bug) diagnostics
  * can only be created via a `const L: Level`-generic constructor, that limits allowed variants via a `where` clause, so not even `rustc_errors` can accidentally bypass this limitation
  * asserts `diagnostic.is_error()` on emission, just in case the construction restriction was bypassed (e.g. by replacing the whole `Diagnostic` inside `DiagnosticBuilder`)
  * `.emit()` returns `ErrorReported`, as a "proof" token that `.emit()` was called
    (though note that this isn't a real guarantee until after completing the work on
     #69426)
* `DiagnosticBuilder<()>` for everything else (warnings, notes, etc.)
  * can also be obtained from other `DiagnosticBuilder`s by calling `.forget_guarantee()`

This PR is a companion to other ongoing work, namely:
* #69426
  and it's ongoing implementation:
  #93222
  the API changes in this PR are needed to get statically-checked "only errors produce `ErrorReported` from `.emit()`", but doesn't itself provide any really strong guarantees without those other `ErrorReported` changes
* #93244
  would make the choices of API changes (esp. naming) in this PR fit better overall

In order to be able to let `.emit()` return anything trustable, several changes had to be made:
* `Diagnostic`'s `level` field is now private to `rustc_errors`, to disallow arbitrary "downgrade"s from "some kind of error" to "warning" (or anything else that doesn't cause compilation to fail)
  * it's still possible to replace the whole `Diagnostic` inside the `DiagnosticBuilder`, sadly, that's harder to fix, but it's unlikely enough that we can paper over it with asserts on `.emit()`
* `.cancel()` now consumes `DiagnosticBuilder`, preventing `.emit()` calls on a cancelled diagnostic
  * it's also now done internally, through `DiagnosticBuilder`-private state, instead of having a `Level::Cancelled` variant that can be read (or worse, written) by the user
  * this removes a hazard of calling `.cancel()` on an error then continuing to attach details to it, and even expect to be able to `.emit()` it
  * warnings were switched to *only* `can_emit_warnings` on emission (instead of pre-cancelling early)
  * `struct_dummy` was removed (as it relied on a pre-`Cancelled` `Diagnostic`)
* since `.emit()` doesn't consume the `DiagnosticBuilder` <sub>(I tried and gave up, it's much more work than this PR)</sub>,
  we have to make `.emit()` idempotent wrt the guarantees it returns
  * thankfully, `err.emit(); err.emit();` can return `ErrorReported` both times, as the second `.emit()` call has no side-effects *only* because the first one did do the appropriate emission
* `&mut Diagnostic` is now used in a lot of function signatures, which used to take `&mut DiagnosticBuilder` (in the interest of not having to make those functions generic)
  * the APIs were already mostly identical, allowing for low-effort porting to this new setup
  * only some of the suggestion methods needed some rework, to have the extra `DiagnosticBuilder` functionality on the `Diagnostic` methods themselves (that change is also present in #93259)
  * `.emit()`/`.cancel()` aren't available, but IMO calling them from an "error decorator/annotator" function isn't a good practice, and can lead to strange behavior (from the caller's perspective)
  * `.downgrade_to_delayed_bug()` was added, letting you convert any `.is_error()` diagnostic into a `delay_span_bug` one (which works because in both cases the guarantees available are the same)

This PR should ideally be reviewed commit-by-commit, since there is a lot of fallout in each.

r? `@estebank` cc `@Manishearth` `@nikomatsakis` `@mark-i-m`
2022-02-25 00:46:04 +00:00
bors 4b043faba3 Auto merge of #94131 - Mark-Simulacrum:fmt-string, r=oli-obk
Always format to internal String in FmtPrinter

This avoids monomorphizing for different parameters, decreasing generic code
instantiated downstream from rustc_middle -- locally seeing 7% unoptimized LLVM IR
line wins on rustc_borrowck, for example.

We likely can't/shouldn't get rid of the Result-ness on most functions, though some
further cleanup avoiding fmt::Error where we now know it won't occur may be possible,
though somewhat painful -- fmt::Write is a pretty annoying API to work with in practice
when you're trying to use it infallibly.
2022-02-24 17:18:07 +00:00
Matthias Krüger 3cd1dc1d6e
Rollup merge of #94270 - RalfJung:fn-ptrs, r=oli-obk
Miri: relax fn ptr check

As discussed in https://github.com/rust-lang/unsafe-code-guidelines/issues/72#issuecomment-1025407536, the function pointer check done by Miri is currently overeager: contrary to our usual principle of only checking rather uncontroversial validity invariants, we actually check that the pointer points to a real function.

So, this relaxes the check to what the validity invariant probably will be (and what the reference already says it is): the function pointer must be non-null, and that's it.

The check that CTFE does on the final value of a constant is unchanged -- CTFE recurses through references, so it makes some sense to also recurse through function pointers. We might still want to relax this in the future, but that would be a separate change.

r? `@oli-obk`
2022-02-24 07:48:06 +01:00
Ralf Jung 182d335870 Miri: relax fn ptr check 2022-02-23 15:11:38 -05:00
Matthias Krüger ecf2faacff
Rollup merge of #94280 - tmiasko:should-print-region, r=oli-obk
Rename `region_should_not_be_omitted` to `should_print_region`

to avoid double negation
2022-02-23 12:26:47 +01:00
Tomasz Miąsko 1113cd5bbe Rename region_should_not_be_omitted to should_print_region
to avoid double negation
2022-02-23 08:58:36 +01:00
Eduard-Mihai Burtescu b7e95dee65 rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
Eduard-Mihai Burtescu 02ff9e0aef Replace &mut DiagnosticBuilder, in signatures, with &mut Diagnostic. 2022-02-23 05:38:19 +00:00
Ralf Jung d0192e6291 Miri: extend comments on downcast operation 2022-02-22 19:09:11 -05:00
Ralf Jung fb1ee8764f ScalarMaybeUninit is explicitly hexadecimal in its formatting 2022-02-21 21:46:51 -05:00
Matthias Krüger f639ba634b
Rollup merge of #94189 - GuillaumeGomez:scalar-lower-hex, r=RalfJung
Implement LowerHex on Scalar to clean up their display in rustdoc

Follow-up of https://github.com/rust-lang/rust/pull/94091.

r? ````@RalfJung````
2022-02-21 19:36:50 +01:00
Matthias Krüger ea7f7f7c4c
Rollup merge of #94143 - est31:let_else_const_eval, r=lcnr
rustc_const_eval: adopt let else in more places

Continuation of #89933, #91018, #91481, #93046, #93590, #94011.

I have extended my clippy lint to also recognize tuple passing and match statements. The diff caused by fixing it is way above 1 thousand lines. Thus, I split it up into multiple pull requests to make reviewing easier. This PR handles rustc_const_eval.
2022-02-21 19:36:48 +01:00
est31 413f3f787c
Fix typo
Co-authored-by: lcnr <rust@lcnr.de>
2022-02-21 08:28:20 +01:00
Mark Rousskov efb99d780d Always format to internal String in FmtPrinter
This avoids monomorphizing for different parameters, decreasing generic code
instantiated downstream from rustc_middle.
2022-02-20 19:32:18 -05:00
bors 523a1b1d38 Auto merge of #94062 - Mark-Simulacrum:drop-print-cfg, r=oli-obk
Move ty::print methods to Drop-based scope guards

Primary goal is reducing codegen of the TLS access for each closure, which shaves ~3 seconds of bootstrap time over rustc as a whole.
2022-02-20 18:12:59 +00:00
Guillaume Gomez c358ffe7b3 Implement LowerHex on Scalar to clean up their display in rustdoc 2022-02-20 16:43:21 +01:00
Tomasz Miąsko c2da477853 Fix pretty printing of enums without variants
92d20c4aad removed no-variants special
case from try_destructure_const with expectation that this case would be
handled gracefully when read_discriminant returns an error.

Alas in that case read_discriminant succeeds while returning a
non-existing variant, so the special case is still necessary.
2022-02-19 17:10:11 +01:00
est31 5cc292eb1d rustc_const_eval: adopt let else in more places 2022-02-19 01:55:47 +01:00