Commit Graph

758 Commits

Author SHA1 Message Date
Nilstrieb
7cf7ead0bc Use line numbers relative to function in mir opt tests
This adds a new option, `-Zmir-pretty-relative-line-numbers`, that
is then used in compiletest for the mir-opt tests.
2022-07-28 11:59:54 +02:00
David Wood
7bab769b58 lint: add bad opt access internal lint
Some command-line options accessible through `sess.opts` are best
accessed through wrapper functions on `Session`, `TyCtxt` or otherwise,
rather than through field access on the option struct in the `Session`.

Adds a new lint which triggers on those options that should be accessed
through a wrapper function so that this is prohibited. Options are
annotated with a new attribute `rustc_lint_opt_deny_field_access` which
can specify the error message (i.e. "use this other function instead")
to be emitted.

A simpler alternative would be to simply rename the options in the
option type so that it is clear they should not be used, however this
doesn't prevent uses, just discourages them. Another alternative would
be to make the option fields private, and adding accessor functions on
the option types, however the wrapper functions sometimes rely on
additional state from `Session` or `TyCtxt` which wouldn't be available
in an function on the option type, so the accessor would simply make the
field available and its use would be discouraged too.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-07-27 11:24:27 +01:00
bors
4d6d601c8a Auto merge of #99574 - durin42:allocator-patch-redux, r=nikic
codegen: use new {re,de,}allocator annotations in llvm

This obviates the patch that teaches LLVM internals about
_rust_{re,de}alloc functions by putting annotations directly in the IR
for the optimizer.

The sole test change is required to anchor FileCheck to the body of the
`box_uninitialized` method, so it doesn't see the `allocalign` on
`__rust_alloc` and get mad about the string `alloca` showing up. Since I
was there anyway, I added some checks on the attributes to prove the
right attributes got set.

r? `@nikic`
2022-07-26 19:35:57 +00:00
Augie Fackler
130a1df71e codegen: use new {re,de,}allocator annotations in llvm
This obviates the patch that teaches LLVM internals about
_rust_{re,de}alloc functions by putting annotations directly in the IR
for the optimizer.

The sole test change is required to anchor FileCheck to the body of the
`box_uninitialized` method, so it doesn't see the `allocalign` on
`__rust_alloc` and get mad about the string `alloca` showing up. Since I
was there anyway, I added some checks on the attributes to prove the
right attributes got set.

While we're here, we also emit allocator attributes on
__rust_alloc_zeroed. This should allow LLVM to perform more
optimizations for zeroed blocks, and probably fixes #90032. [This
comment](https://github.com/rust-lang/rust/issues/24194#issuecomment-308791157)
mentions "weird UB-like behaviour with bitvec iterators in
rustc_data_structures" so we may need to back this change out if things
go wrong.

The new test cases require LLVM 15, so we copy them into LLVM
14-supporting versions, which we can delete when we drop LLVM 14.
2022-07-26 09:43:28 -04:00
Yuki Okushi
85afb90788
Rollup merge of #99718 - TaKO8Ki:avoid-&str-symbol-to-string-conversions, r=michaelwoerister
Avoid `&str`/`Symbol` to `String` conversions

follow-up to #99342 and #98668
2022-07-26 13:12:23 +09:00
Takayuki Maeda
bf1a5e72f4 remove is_local_span as it is no longer used 2022-07-25 22:37:14 +09:00
bors
7f93d4aa0d Auto merge of #98770 - klensy:no-string-dupes-ugly, r=cjgillot
rmeta: avoid embedding `StabilityLevel::Unstable` reason multiple times into .rmeta\.rlib files

Avoids bloating size of some rmeta\rlib files by not placing default string for `StabilityLevel::Unstable` reason multiple times, affects only stdlib\rustc artifacts. For stdlib cuts about 3% (diff of total size for patched\unpatched *.rmeta files of stage1-std) of file size, depending on crates.

fixes #88180
2022-07-25 05:27:17 +00:00
bors
93ffde6f04 Auto merge of #98208 - ivanloz:master, r=nagisa
Add support for LLVM ShadowCallStack.

LLVMs ShadowCallStack provides backward edge control flow integrity protection by using a separate shadow stack to store and retrieve a function's return address.

LLVM currently only supports this for AArch64 targets. The x18 register is used to hold the pointer to the shadow stack, and therefore this only works on ABIs which reserve x18. Further details are available in the [LLVM ShadowCallStack](https://clang.llvm.org/docs/ShadowCallStack.html) docs.

# Usage
`-Zsanitizer=shadow-call-stack`

# Comments/Caveats
* Currently only enabled for the aarch64-linux-android target
* Requires the platform to define a runtime to initialize the shadow stack, see the [LLVM docs](https://clang.llvm.org/docs/ShadowCallStack.html) for more detail.
2022-07-23 20:01:07 +00:00
bors
aa01891700 Auto merge of #99420 - RalfJung:vtable, r=oli-obk
make vtable pointers entirely opaque

This implements the scheme discussed in https://github.com/rust-lang/unsafe-code-guidelines/issues/338: vtable pointers should be considered entirely opaque and not even readable by Rust code, similar to function pointers.

- We have a new kind of `GlobalAlloc` that symbolically refers to a vtable.
- Miri uses that kind of allocation when generating a vtable.
- The codegen backends, upon encountering such an allocation, call `vtable_allocation` to obtain an actually dataful allocation for this vtable.
- We need new intrinsics to obtain the size and align from a vtable (for some `ptr::metadata` APIs), since direct accesses are UB now.

I had to touch quite a bit of code that I am not very familiar with, so some of this might not make much sense...
r? `@oli-obk`
2022-07-22 01:33:49 +00:00
klensy
b38c94857d avoid embedding StabilityLevel::Unstable reason string into metadata multiple times 2022-07-21 22:53:02 +03:00
Ralf Jung
8affef2ccb add intrinsic to access vtable size and align 2022-07-20 17:12:07 -04:00
David Wood
97edb9f336 span: add span_extend_to_line helper
Adds a simple helper function to the `SourceMap` for extending a `Span`
to encompass the entire line it is on - useful for suggestions where
removing a line is the suggested action.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-07-20 14:53:01 +01:00
David Wood
224aec213d middle: add implies_by to #[unstable]
If part of a feature is stabilized and a new feature is added for the
remaining parts, then the `implied_by` attribute can be used to indicate
which now-stable feature previously contained a item. If the now-stable
feature is still active (if the user has only just updated rustc, for
example) then there will not be an stability error for uses of the item
from the implied feature.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-07-20 14:53:01 +01:00
Ivan Lozano
adf61e3b2b Add ShadowCallStack Support
Adds support for the LLVM ShadowCallStack sanitizer.
2022-07-20 13:43:34 +00:00
bors
9a7b7d5e50 Auto merge of #98180 - notriddle:notriddle/rustdoc-fn, r=petrochenkov,GuillaumeGomez
Improve the function pointer docs

This is #97842 but for function pointers instead of tuples. The concept is basically the same.

* Reduce duplicate impls; show `fn (T₁, T₂, …, Tₙ)` and include a sentence saying that there exists up to twelve of them.
* Show `Copy` and `Clone`.
* Show auto traits like `Send` and `Sync`, and blanket impls like `Any`.

https://notriddle.com/notriddle-rustdoc-test/std/primitive.fn.html
2022-07-19 19:36:57 +00:00
Matthias Krüger
e6a100baa2
Rollup merge of #99438 - WaffleLapkin:dont_wrap_in_non_zero, r=compiler-errors
Improve suggestions for `NonZeroT` <- `T` coercion error

Currently, when encountering a type mismatch error with `NonZeroT` and `T` (for example `NonZeroU8` and `u8`) we errorneusly suggest wrapping expression in `NonZeroT`:
```text
error[E0308]: mismatched types
 --> ./t.rs:7:35
  |
7 |     let _: std::num::NonZeroU64 = 1;
  |            --------------------   ^ expected struct `NonZeroU64`, found integer
  |            |
  |            expected due to this
  |
help: try wrapping the expression in `std::num::NonZeroU64`
  |
7 |     let _: std::num::NonZeroU64 = std::num::NonZeroU64(1);
  |                                   +++++++++++++++++++++ +
```

I've removed this suggestion and added suggestions to call `new` (for `Option<NonZeroT>` <- `T` case) or `new` and `unwrap` (for `NonZeroT` <- `T` case):

```text
error[E0308]: mismatched types
 --> ./t.rs:7:35
  |
7 |     let _: std::num::NonZeroU64 = 1;
  |            --------------------   ^ expected struct `NonZeroU64`, found integer
  |            |
  |            expected due to this
  |
help: Consider calling `NonZeroU64::new`
  |
7 |     let _: std::num::NonZeroU64 = NonZeroU64::new(1).unwrap();
  |                                   ++++++++++++++++ ++++++++++

error[E0308]: mismatched types
 --> ./t.rs:8:43
  |
8 |     let _: Option<std::num::NonZeroU64> = 1;
  |            ----------------------------   ^ expected enum `Option`, found integer
  |            |
  |            expected due to this
  |
  = note: expected enum `Option<NonZeroU64>`
             found type `{integer}`
help: Consider calling `NonZeroU64::new`
  |
8 |     let _: Option<std::num::NonZeroU64> = NonZeroU64::new(1);
  |                                           ++++++++++++++++ +
```

r? `@compiler-errors`
2022-07-19 13:30:49 +02:00
Matthias Krüger
4815f94c51
Rollup merge of #99401 - TaKO8Ki:avoid-symbol-to-&str-conversions, r=nnethercote
Avoid `Symbol` to `&str` conversions

`Symbol::as_str` is a slowish operation, so this patch removes some usages of it.
2022-07-19 13:30:46 +02:00
Maybe Waffle
7163e7ff65 Suggest a fix for NonZero* <- * coercion error 2022-07-19 00:13:29 +04:00
Takayuki Maeda
a22934bea1 avoid Symbol to &str conversions 2022-07-18 14:25:34 +09:00
Michael Howell
1169832f2f rustdoc: extend #[doc(tuple_variadic)] to fn pointers
The attribute is also renamed `fake_variadic`.
2022-07-17 16:32:06 -07:00
Dylan DPC
24f0e1499e
Rollup merge of #99119 - TaKO8Ki:remove-string-matching-about-methods, r=cjgillot
Refactor: remove a string matching about methods

This patch remove a string matching about methods and adds some rustfix tests.
2022-07-15 15:53:38 +05:30
Takayuki Maeda
45b88aff10 simplify suggest_deref_ref_or_into 2022-07-15 14:29:15 +09:00
bors
24699bcbad Auto merge of #95956 - yaahc:stable-in-unstable, r=cjgillot
Support unstable moves via stable in unstable items

part of https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/moving.20items.20to.20core.20unstably and a blocker of https://github.com/rust-lang/rust/pull/90328.

The libs-api team needs the ability to move an already stable item to a new location unstably, in this case for Error in core. Otherwise these changes are insta-stable making them much harder to merge.

This PR attempts to solve the problem by checking the stability of path segments as well as the last item in the path itself, which is currently the only thing checked.
2022-07-14 13:42:09 +00:00
bors
f1a8854f9b Auto merge of #99231 - Dylan-DPC:rollup-0tl8c0o, r=Dylan-DPC
Rollup of 5 pull requests

Successful merges:

 - #97720 (Always create elided lifetime parameters for functions)
 - #98315 (Stabilize `core::ffi:c_*` and rexport in `std::ffi`)
 - #98705 (Implement `for<>` lifetime binder for closures)
 - #99126 (remove allow(rustc::potential_query_instability) in rustc_span)
 - #99139 (Give a better error when `x dist` fails for an optional tool)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-07-14 11:00:30 +00:00
Dylan DPC
85159a4df8
Rollup merge of #99126 - NiklasJonsson:84447/rustc_span, r=petrochenkov
remove allow(rustc::potential_query_instability) in rustc_span

Also, avoid sorting before debug output as iteration order can now be
relied upon.

Related #84447
2022-07-14 14:14:22 +05:30
Dylan DPC
e5a86d7358
Rollup merge of #98705 - WaffleLapkin:closure_binder, r=cjgillot
Implement `for<>` lifetime binder for closures

This PR implements RFC 3216 ([TI](https://github.com/rust-lang/rust/issues/97362)) and allows code like the following:

```rust
let _f = for<'a, 'b> |a: &'a A, b: &'b B| -> &'b C { b.c(a) };
//       ^^^^^^^^^^^--- new!
```

cc ``@Aaron1011`` ``@cjgillot``
2022-07-14 14:14:21 +05:30
Dylan DPC
f5e9cb53ab
Rollup merge of #97720 - cjgillot:all-fresh, r=petrochenkov
Always create elided lifetime parameters for functions

Anonymous and elided lifetimes in functions are sometimes (async fns) --and sometimes not (regular fns)-- desugared to implicit generic parameters.

This difference of treatment makes it some downstream analyses more complicated to handle.  This step is a pre-requisite to perform lifetime elision resolution on AST.

There is currently an inconsistency in the treatment of argument-position impl-trait for functions and async fns:
```rust
trait Foo<'a> {}
fn foo(t: impl Foo<'_>) {} //~ ERROR missing lifetime specifier
async fn async_foo(t: impl Foo<'_>) {} //~ OK
fn bar(t: impl Iterator<Item = &'_ u8>) {} //~ ERROR missing lifetime specifier
async fn async_bar(t: impl Iterator<Item = &'_ u8>) {} //~ OK
```

The current implementation reports "missing lifetime specifier" on `foo`, but **accepts it** in `async_foo`.
This PR **proposes to accept** the anonymous lifetime in both cases as an extra generic lifetime parameter.
This change would be insta-stable, so let's ping t-lang.
Anonymous lifetimes in GAT bindings keep being forbidden:
```rust
fn foo(t: impl Foo<Assoc<'_> = Bar<'_>>) {}
                         ^^        ^^
                       forbidden   ok
```
I started a discussion here: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Anonymous.20lifetimes.20in.20universal.20impl-trait/near/284968606

r? ``@petrochenkov``
2022-07-14 14:14:19 +05:30
Joshua Nelson
3c9765cff1 Rename debugging_opts to unstable_opts
This is no longer used only for debugging options (e.g. `-Zoutput-width`, `-Zallow-features`).
Rename it to be more clear.
2022-07-13 17:47:06 -05:00
Niklas Jonsson
f94484fe7e reduce scope of allow(rustc::potential_query_instability) in rustc_span 2022-07-13 19:30:08 +02:00
Dylan DPC
1e7d04b23b
Rollup merge of #99011 - oli-obk:UnsoundCell, r=eddyb
`UnsafeCell` blocks niches inside its nested type from being available outside

fixes #87341

This implements the plan by `@eddyb` in https://github.com/rust-lang/rust/issues/87341#issuecomment-886083646

Somewhat related PR (not strictly necessary, but that cleanup made this PR simpler): #94527
2022-07-13 19:32:34 +05:30
Camille GILLOT
5a20834884 Add feature gate. 2022-07-13 14:17:09 +02:00
Maybe Waffle
40ae7b5b8e Parse closure binders
This is first step in implementing RFC 3216.
- Parse `for<'a>` before closures in ast
  - Error in lowering
- Add `closure_lifetime_binder` feature
2022-07-12 16:25:16 +04:00
Ding Xiang Fei
6c529ded86
lower let-else in MIR instead 2022-07-11 23:20:36 +02:00
Dylan DPC
9fc297a2ae
Rollup merge of #99140 - TaKO8Ki:implement-is-accessible-span, r=fee1-dead
Implement `SourceMap::is_span_accessible`

This patch adds `SourceMap::is_span_accessible` and replaces `span_to_snippet(span).is_ok()` and `span_to_snippet(span).is_err()` with it. This removes a `&str` to `String` conversion.
2022-07-11 15:19:32 +05:30
Takayuki Maeda
018155c3a2 rename a method 2022-07-11 16:51:19 +09:00
Takayuki Maeda
12d11e9a35 implement is_accessible_span 2022-07-11 11:36:15 +09:00
Matthias Krüger
86af7135ae
Rollup merge of #99103 - TaKO8Ki:avoid-&str-to-string-conversions, r=oli-obk
Avoid some `&str` to `String` conversions

This patch removes some `&str` to `String` conversions.
2022-07-10 11:52:17 +02:00
Takayuki Maeda
bda83e6543 avoid some &str to String conversions 2022-07-10 03:18:56 +09:00
bors
86b8dd5389 Auto merge of #99028 - tmiasko:inline, r=estebank
Miscellaneous inlining improvements

Add `#[inline]` to a few trivial non-generic methods from a perf report
that otherwise wouldn't be candidates for inlining.
2022-07-09 04:34:51 +00:00
Jane Lusby
b55453dbad add opt in attribute for stable-in-unstable items 2022-07-08 21:18:15 +00:00
bors
1dcff2d507 Auto merge of #98638 - bjorn3:less_string_interning, r=tmiasko
Use less string interning

This removes string interning in a couple of places where doing so won't result in perf improvements. I also switched one place to use pre-interned symbols.
2022-07-08 10:03:27 +00:00
Tomasz Miąsko
87374de3ad Miscellaneous inlining improvements
Add `#[inline]` to a few trivial non-generic methods from a perf report
that otherwise wouldn't be candidates for inlining.
2022-07-07 22:20:08 +02:00
Oli Scherer
2a899dc1cf UnsafeCell now has no niches, ever. 2022-07-07 10:46:22 +00:00
David Wood
540eaf985d errors: introduce DecorateLint
Add a new trait to be generated by diagnostic derives which uses a
`LintDiagnosticBuilder`.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-07-05 16:00:20 +01:00
bors
126e3df406 Auto merge of #98376 - nnethercote:improve-derive-PartialEq, r=petrochenkov
Improve some deriving code and add a test

The `.stdout` test is particularly useful.

r? `@petrochenkov`
2022-06-29 00:20:57 +00:00
bjorn3
de57dbcefd Use pre-interned symbols in import recovery 2022-06-28 18:38:36 +00:00
Matthias Krüger
a1b06388ce
Rollup merge of #98609 - TaKO8Ki:fix-ice-for-associated-constant-generics, r=lcnr
Fix ICE for associated constant generics

Fixes #98432
2022-06-28 18:34:31 +02:00
Takayuki Maeda
8c7d9f6092 fix ice for associated constant generics 2022-06-28 18:29:07 +09:00
Nicholas Nethercote
b7855fa9de Factor out the repeated assert_ty_bounds function. 2022-06-27 08:14:09 +10:00
Nicholas Nethercote
5b54363961 Optimize the code produced by derive(Debug).
This commit adds new methods that combine sequences of existing
formatting methods.
- `Formatter::debug_{tuple,struct}_field[12345]_finish`, equivalent to a
  `Formatter::debug_{tuple,struct}` + N x `Debug{Tuple,Struct}::field` +
  `Debug{Tuple,Struct}::finish` call sequence.
- `Formatter::debug_{tuple,struct}_fields_finish` is similar, but can
  handle any number of fields by using arrays.

These new methods are all marked as `doc(hidden)` and unstable. They are
intended for the compiler's own use.

Special-casing up to 5 fields gives significantly better performance
results than always using arrays (as was tried in #95637).

The commit also changes the `Debug` deriving code to use these new methods. For
example, where the old `Debug` code for a struct with two fields would be like
this:
```
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
    match *self {
	Self {
	    f1: ref __self_0_0,
	    f2: ref __self_0_1,
	} => {
	    let debug_trait_builder = &mut ::core::fmt::Formatter::debug_struct(f, "S2");
	    let _ = ::core::fmt::DebugStruct::field(debug_trait_builder, "f1", &&(*__self_0_0));
	    let _ = ::core::fmt::DebugStruct::field(debug_trait_builder, "f2", &&(*__self_0_1));
	    ::core::fmt::DebugStruct::finish(debug_trait_builder)
	}
    }
}
```
the new code is like this:
```
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
    match *self {
	Self {
	    f1: ref __self_0_0,
	    f2: ref __self_0_1,
	} => ::core::fmt::Formatter::debug_struct_field2_finish(
	    f,
	    "S2",
	    "f1",
	    &&(*__self_0_0),
	    "f2",
	    &&(*__self_0_1),
	),
    }
}
```
This shrinks the code produced for `Debug` instances
considerably, reducing compile times and binary sizes.

Co-authored-by: Scott McMurray <scottmcm@users.noreply.github.com>
2022-06-24 09:40:15 +10:00