Commit Graph

1455 Commits

Author SHA1 Message Date
bors
e9c965df7b Auto merge of #128812 - nnethercote:shrink-TyKind-FnPtr, r=compiler-errors
Shrink `TyKind::FnPtr`.

By splitting the `FnSig` within `TyKind::FnPtr` into `FnSigTys` and `FnHeader`, which can be packed more efficiently. This reduces the size of the hot `TyKind` type from 32 bytes to 24 bytes on 64-bit platforms. This reduces peak memory usage by a few percent on some benchmarks. It also reduces cache misses and page faults similarly, though this doesn't translate to clear cycles or wall-time improvements on CI.

r? `@compiler-errors`
2024-08-14 00:56:53 +00:00
Guillaume Gomez
ea74eff55c
Rollup merge of #128886 - GrigorenkoPV:untranslatable-diagnostic, r=nnethercote
Get rid of some `#[allow(rustc::untranslatable_diagnostic)]`

`@rustbot` label +A-translation
cc https://github.com/rust-lang/rust/issues/100717
2024-08-12 17:09:17 +02:00
Matthias Krüger
32e0fe129d
Rollup merge of #128762 - fmease:use-more-slice-pats, r=compiler-errors
Use more slice patterns inside the compiler

Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'.

r? ghost
2024-08-11 07:51:51 +02:00
Pavel Grigorenko
f09a2b047d rustc_passes: make some messages in check_attr translatable 2024-08-10 14:32:56 +03:00
Pavel Grigorenko
fcdb37435a rustc_passes: remove a redundant #[allow(rustc::untranslatable_diagnostic)] 2024-08-10 14:32:56 +03:00
Nicholas Nethercote
c4717cc9d1 Shrink TyKind::FnPtr.
By splitting the `FnSig` within `TyKind::FnPtr` into `FnSigTys` and
`FnHeader`, which can be packed more efficiently. This reduces the size
of the hot `TyKind` type from 32 bytes to 24 bytes on 64-bit platforms.
This reduces peak memory usage by a few percent on some benchmarks. It
also reduces cache misses and page faults similarly, though this doesn't
translate to clear cycles or wall-time improvements on CI.
2024-08-09 14:33:25 +10:00
Trevor Gross
6c2e06746d
Rollup merge of #128552 - s7tya:check-no-sanitize-attribute-pos, r=BoxyUwU
Emit an error for invalid use of the `#[no_sanitize]` attribute

fixes #128487.

Currently, the use of the `#[no_sanitize]` attribute for Mod, Impl,... is incorrectly permitted. This PR will correct this issue by generating errors, and I've also added some UI test cases for it.

Referenced #128458. As far as I know, the `#[no_sanitize]` attribute can only be used with functions, so I changed that part to `Fn` and `Method` using `check_applied_to_fn_or_method`. However, I couldn't find explicit documentation on this, so I could be mistaken...
2024-08-07 20:49:02 -05:00
León Orell Valerian Liehr
c4c518d2d4
Use more slice patterns inside the compiler 2024-08-07 13:37:52 +02:00
许杰友 Jieyou Xu (Joe)
97cbc2083f check_attr: treat cfg_attr like cfg 2024-08-06 06:12:10 +00:00
Shina
61ea488309 Emit an error for invalid use of the #[no_sanitize] attribute 2024-08-05 19:07:32 +09:00
许杰友 Jieyou Xu (Joe)
9d0eaa2ad7 check_attr: cover multi-segment attributes on specific check arms
PR #128581 introduced an assertion that all builtin attributes are
actually checked via `CheckAttrVisitor` and aren't accidentally usable
on completely unrelated HIR nodes. Unfortunately, the check had
correctness problems.

The match on attribute path segments looked like

```rust,ignore
[sym::should_panic] => /* check is implemented */
match BUILTIN_ATTRIBUTE_MAP.get(name) {
    // checked below
    Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) => {}
    Some(_) => {
        if !name.as_str().starts_with("rustc_") {
            span_bug!(
                attr.span,
                "builtin attribute {name:?} not handled by `CheckAttrVisitor`"
            )
        }
    }
    None => (),
}
```

However, it failed to account for edge cases such as an attribute whose:

1. path segments *starts* with a builtin attribute such as
   `should_panic`
2. which does not start with `rustc_`, and
3. is also an `AttributeType::Normal` attribute upon registration with
   the builtin attribute map

These conditions when all satisfied cause the span bug to be issued for e.g.
`#[should_panic::skip]` because the `[sym::should_panic]` arm is not matched (since it's
`[sym::should_panic, sym::skip]`).

See <https://github.com/rust-lang/rust/issues/128622>.
2024-08-04 01:50:55 +00:00
Matthias Krüger
3a9d432589
Rollup merge of #128581 - jieyouxu:checked-attr, r=nnethercote
Assert that all attributes are actually checked via `CheckAttrVisitor` and aren't accidentally usable on completely unrelated HIR nodes

``@oli-obk's`` #128444 with unreachable case removed to avoid that PR bitrotting away.
Based on #128402.

This PR will make adding a new attribute ICE on any use of that attribute unless it gets a handler added in `rustc_passes::CheckAttrVisitor`.

r? ``@nnethercote`` (since you were the reviewer of the original PR)
2024-08-03 20:51:54 +02:00
Michael Goulet
ac56007ea7 Revert "Rollup merge of #125572 - mu001999-contrib:dead/enhance, r=pnkfelix"
This reverts commit 13314df21b, reversing
changes made to 6e534c73c3.
2024-08-03 07:57:31 -04:00
Michael Goulet
d29818c9f5 Revert "Rollup merge of #126315 - mu001999-contrib:fix/126289, r=petrochenkov"
This reverts commit 977c5fd419, reversing
changes made to 24c94f0e4f.
2024-08-03 07:57:31 -04:00
Michael Goulet
22da616245 Revert "Rollup merge of #126618 - mu001999-contrib:dead/enhance, r=pnkfelix"
This reverts commit 2724aeaaeb, reversing
changes made to d929a42a66.
2024-08-03 07:57:31 -04:00
Michael Goulet
5f5b4ee128 Revert "Rollup merge of #127107 - mu001999-contrib:dead/enhance-2, r=pnkfelix"
This reverts commit 31fe9628cf, reversing
changes made to f20307851e.
2024-08-03 07:57:31 -04:00
Michael Goulet
c6b6c1270a Revert "Rollup merge of #127017 - mu001999-contrib:dead/enhance, r=pnkfelix"
This reverts commit a70dc297a8, reversing
changes made to ceae37188b.
2024-08-03 07:57:31 -04:00
Michael Goulet
361ab1af0c Revert "Rollup merge of #128104 - mu001999-contrib:fix/128053, r=petrochenkov"
This reverts commit 91b18a058c, reversing
changes made to 9aedec9313.
2024-08-03 07:57:30 -04:00
Oli Scherer
ed010dd32c Assert that all attributes are actually checked via CheckAttrVisitor and aren't accidentally usable on completely unrelated HIR nodes
Co-authored-by: Jieyou Xu <jieyouxu@outlook.com>
2024-08-03 02:26:21 +00:00
clubby789
6d7bb127e6 Emit an error if #[optimize] is applied to an incompatible item 2024-07-31 20:28:00 +00:00
Matthias Krüger
e2d8f1ac21
Rollup merge of #128402 - oli-obk:checked_attrs, r=compiler-errors
Attribute checking simplifications

remove an unused boolean and then merge two big matches into one

I was reviewing some attributes and realized we don't really check this list against the list of builtin attributes, so we "may" totally be missing some attributes that we should be checking (like the `coroutine` attribute, which you can just apply to random stuff)

```rust
#![feature(coroutines)]
#[coroutine]
struct Foo;
```

just compiles for example. Unless we check that the fallthrough match arm is never reached for builtin attributes, we're just going to keep forgetting to add them here, too. I can do that without the changes in this PR, but it seemed like a nice cleanup
2024-07-31 15:36:32 +02:00
Matthias Krüger
e6a82d2878
Rollup merge of #128380 - folkertdev:naked-compatible-doc-comment, r=bjorn3
make `///` doc comments compatible with naked functions

tracking issue: https://github.com/rust-lang/rust/issues/90957

reported in https://github.com/rust-lang/rust/pull/127853#issuecomment-2257323333

it turns out `/// doc comment` and `#[doc = "doc comment"]` are represented differently, at least at the point where we perform the check for what should be allowed. The `///` style doc comment is now also allowed.

r? ``@bjorn3``

cc ``@hsanzg``
2024-07-30 22:51:39 +02:00
Oli Scherer
67a08b5deb Attribute checking simplifications
remove an unused boolean and then merge two big matches into one
2024-07-30 15:02:40 +00:00
Folkert
58bfd98baf
make /// doc comments compatible with naked functions 2024-07-30 12:20:18 +02:00
Matthias Krüger
91b18a058c
Rollup merge of #128104 - mu001999-contrib:fix/128053, r=petrochenkov
Not lint pub structs without pub constructors intentionally

Fixes #128053
2024-07-29 21:26:12 +02:00
Nicholas Nethercote
84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Matthias Krüger
a13f40dae6
Rollup merge of #127853 - folkertdev:naked-function-error-messages, r=bjorn3
`#[naked]`: report incompatible attributes

tracking issue: https://github.com/rust-lang/rust/issues/90957

this is a re-implementation of https://github.com/rust-lang/rust/pull/93809 by ``@bstrie`` which was closed 2 years ago due to inactivity.

This PR takes some of the final comments into account, specifically providing a little more context in error messages, and using an allow list to determine which attributes are compatible with `#[naked]`.

Notable attributes that are incompatible with `#[naked]` are:

  * `#[inline]`
  * `#[track_caller]`
  * ~~`#[target_feature]`~~ (this is now allowed, see PR discussion)
  * `#[test]`, `#[ignore]`, `#[should_panic]`

These attributes just directly conflict with what `#[naked]` should do.

Naked functions are still important for systems programming, embedded, and operating systems, so I'd like to move them forward.
2024-07-28 08:57:16 +02:00
Trevor Gross
9164dbd48c
Rollup merge of #128207 - folkertdev:asm-parser-generalize, r=Amanieu
improve error message when `global_asm!` uses `asm!` options

specifically, what was

    error: expected one of `)`, `att_syntax`, or `raw`, found `preserves_flags`
      --> $DIR/bad-options.rs:45:25
       |
    LL | global_asm!("", options(preserves_flags));
       |                         ^^^^^^^^^^^^^^^ expected one of `)`, `att_syntax`, or `raw`

is now

    error: the `preserves_flags` option cannot be used with `global_asm!`
      --> $DIR/bad-options.rs:45:25
       |
    LL | global_asm!("", options(preserves_flags));
       |                         ^^^^^^^^^^^^^^^ the `preserves_flags` option is not meaningful for global-scoped inline assembly

mirroring the phrasing of the [reference](https://doc.rust-lang.org/reference/inline-assembly.html#options).

This is also a bit of a refactor for a future `naked_asm!` macro (for use in `#[naked]` functions). Currently this sort of error can come up when switching from inline to global asm, or when a user just isn't that experienced with assembly. With  `naked_asm!` added to the mix hitting this error is more likely.
2024-07-27 13:32:56 -04:00
Folkert
a3bb0104ff
allow #[target_feature] on #[naked] functions 2024-07-27 12:56:20 +02:00
Folkert
c6a166bac2
switch to an allowlist approach
- merge error codes
- use attribute name that is incompatible in error message
- add test for conditional incompatible attribute
- add `linkage` to the allowlist
2024-07-27 12:55:39 +02:00
mu001999
adf0dff10c Not lint pub structs without pub constructors if containing fields of unit, never type, PhantomData and positional ZST 2024-07-26 21:56:35 +08:00
Trevor Gross
a70dc297a8
Rollup merge of #127017 - mu001999-contrib:dead/enhance, r=pnkfelix
Extend rules of dead code analysis for impls for adts to impls for types refer to adts

The rules of dead code analysis for impl blocks can be extended to self types which refer to adts.

So that we can lint the following unused struct and trait:
```rust
struct Foo; //~ ERROR struct `Foo` is never constructed

trait Trait { //~ ERROR trait `Trait` is never used
    fn foo(&self) {}
}

impl Trait for &Foo {}
```

r? `@pnkfelix`
2024-07-26 02:20:29 -04:00
Folkert
d3858f7465
improve error message when global_asm! uses asm! options 2024-07-25 22:33:52 +02:00
Matthias Krüger
e76bb3fab6
Rollup merge of #128138 - folkertdev:asm-option-allowlist, r=lcnr
`#[naked]`: use an allowlist for allowed options on `asm!` in naked functions

tracking issue: https://github.com/rust-lang/rust/issues/90957

this is mostly just a refactor, but using an allowlist (rather than a denylist) for which asm options are allowed in naked functions is a little safer.

These options are disallowed because naked functions are effectively global asm, but defined using inline asm.
2024-07-25 16:48:20 +02:00
Folkert
4b7a87de10
use an allow list for allowed asm options in naked functions 2024-07-24 15:27:56 +02:00
Michael Goulet
ce8a625092 Move all error reporting into rustc_trait_selection 2024-07-21 22:34:35 -04:00
Noah Lev
11b144aa98 hir: Create hir::ConstArgKind enum
This will allow lowering const params to a dedicated enum variant, rather
than to an `AnonConst` that is later examined during `ty` lowering.
2024-07-16 19:27:28 -07:00
Folkert
7e6c083873
improve error message when #[naked] is used with #[track-caller] and #[target-feature]`` 2024-07-16 23:35:02 +02:00
Folkert
4bd36324b6
improve error message when #[naked] is used with #[inline] 2024-07-16 23:03:13 +02:00
Michael Goulet
153a381104 Report usage of lib features in ast validation 2024-07-10 16:53:41 -04:00
Michael Goulet
fe4c995ccb Move trait selection error reporting to its own top-level module 2024-07-08 16:04:47 -04:00
Michael Goulet
31fe9628cf
Rollup merge of #127107 - mu001999-contrib:dead/enhance-2, r=pnkfelix
Improve dead code analysis

Fixes #120770

1. check impl items later if self ty is private although the trait method is public, cause we must use the ty firstly if it's private
2. mark the adt live if it appears in pattern, like generic argument, this implies the use of the adt
3. based on the above, we can handle the case that private adts impl Default, so that we don't need adding rustc_trivial_field_reads on Default, and the logic in should_ignore_item

r? ``@pnkfelix``
2024-07-05 20:49:31 -04: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
mu001999
0adb82528f Improve dead code analysis 2024-07-04 22:05:00 +08:00
Matthias Krüger
33e9f25e91
Rollup merge of #127092 - compiler-errors:rtn-dots-redux, r=estebank
Change return-type-notation to use `(..)`

Aligns the syntax with the current wording of [RFC 3654](https://github.com/rust-lang/rfcs/pull/3654). Also implements rustfmt support (along with making a match exhaustive).

Tracking:
* https://github.com/rust-lang/rust/issues/109417
2024-07-03 23:30:07 +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
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
Matthias Krüger
5ea1a03cca
Rollup merge of #127118 - surechen:fix_126789, r=jieyouxu
Show `used attribute`'s kind for user when find it isn't applied to a `static` variable.

For example :
```rust
extern "C" {
    #[used] //~ ERROR attribute must be applied to a `static` variable
    static FOO: i32; // show the kind of this item to help user understand why the error is reported.
}
```

fixes #126789
2024-06-29 22:10:59 +02:00