Commit graph

1730 commits

Author SHA1 Message Date
Folkert de Vries
df8a3d5f1d
stabilize naked_functions 2025-04-20 11:18:38 +02:00
Folkert de Vries
41ddf86722
Make #[naked] an unsafe attribute 2025-04-19 00:03:35 +02:00
Matthias Krüger
540fb228af
Rollup merge of #139615 - nnethercote:rm-name_or_empty, r=jdonszelmann
Remove `name_or_empty`

Another step towards #137978.

r? ``@jdonszelmann``
2025-04-18 05:16:29 +02:00
Matthias Krüger
059b7d07ef
Rollup merge of #139944 - shepmaster:eager-diagnostics, r=nnethercote
Move eager translation to a method on Diag

This will allow us to eagerly translate messages on a top-level diagnostic, such as a `LintDiagnostic`. As a bonus, we can remove the awkward closure passed into Subdiagnostic and make better use of `Into`.

r? `@nnethercote`
2025-04-17 06:25:18 +02:00
Matthias Krüger
0757d24f71
Rollup merge of #139782 - xizheyin:issue-139627, r=wesleywiser
Consistent with treating Ctor Call as Struct in liveness analysis

Fixes #139627

When `ExprKind::Call` is a `Ctor`, skips the checking of `expr` and only checks the arguments, thus being consistent with `ExprKind::Struct`.

r? compiler
2025-04-17 06:25:16 +02:00
Jake Goulding
0117884917 Move eager translation to a method on Diag
This will allow us to eagerly translate messages on a top-level
diagnostic, such as a `LintDiagnostic`. As a bonus, we can remove the
awkward closure passed into Subdiagnostic and make better use of
`Into`.
2025-04-16 21:38:59 -04:00
Nicholas Nethercote
2fef0a30ae Replace infallible name_or_empty methods with fallible name methods.
I'm removing empty identifiers everywhere, because in practice they
always mean "no identifier" rather than "empty identifier". (An empty
identifier is impossible.) It's better to use `Option` to mean "no
identifier" because you then can't forget about the "no identifier"
possibility.

Some specifics:
- When testing an attribute for a single name, the commit uses the
  `has_name` method.
- When testing an attribute for multiple names, the commit uses the new
  `has_any_name` method.
- When using `match` on an attribute, the match arms now have `Some` on
  them.

In the tests, we now avoid printing empty identifiers by not printing
the identifier in the `error:` line at all, instead letting the carets
point out the problem.
2025-04-17 09:50:52 +10:00
Nicholas Nethercote
400e8e5dc8 Fix attribute printing in an error.
The current code assumes that the attribute is just an identifier, and
so misprints paths.
2025-04-17 09:50:52 +10:00
Yotam Ofek
4b63362f3d Use newtype_index!-generated types more idiomatically 2025-04-14 16:17:06 +00:00
xizheyin
8c8212ef12
Consistent with treating Ctor Call as Struct in liveness analysis
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-04-14 14:29:14 +08:00
bors
f836ae4e66 Auto merge of #124141 - nnethercote:rm-Nonterminal-and-TokenKind-Interpolated, r=petrochenkov
Remove `Nonterminal` and `TokenKind::Interpolated`

A third attempt at this; the first attempt was #96724 and the second was #114647.

r? `@ghost`
2025-04-14 03:56:55 +00:00
Jacob Pratt
7f691d28f1
Rollup merge of #139001 - folkertdev:naked-function-rustic-abi, r=traviscross,compiler-errors
add `naked_functions_rustic_abi` feature gate

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

Because the details of the rust abi are unstable, and a naked function must match its stated ABI, this feature gate keeps naked functions with a rustic abi ("Rust", "rust-cold", "rust-call" and "rust-intrinsic") unstable.

r? ````@traviscross````
2025-04-13 17:37:52 -04:00
Michael Howell
e013cf8afc rustdoc-search: add unbox flag to Result aliases
Fixes #139665
2025-04-11 11:36:40 -07:00
Folkert de Vries
8866af3884
Add naked_functions_rustic_abi feature gate 2025-04-07 21:42:12 +02:00
Stuart Cook
9209c5eb60
Rollup merge of #139455 - Skgland:remove_rust-intrinsic_ABI, r=oli-obk
Remove support for `extern "rust-intrinsic"` blocks

Part of rust-lang/rust#132735

Looked manageable and there didn't appear to have been progress in the last two weeks,
so decided to give it a try.
2025-04-07 22:29:20 +10:00
Stuart Cook
82df6229b6
Rollup merge of #139035 - nnethercote:PatKind-Missing, r=oli-obk
Add new `PatKind::Missing` variants

To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with #137978. Details in the individual commits.

r? ``@oli-obk``
2025-04-07 22:29:17 +10:00
Michael Goulet
c51c448fae More trivial tweaks 2025-04-07 05:07:11 +00:00
Skgland
5eb535c568
remove compiler support for extern "rust-intrinsic" blocks 2025-04-06 21:32:58 +02:00
Oli Scherer
57c4ab7ff5 impl !PartialOrd for HirId 2025-04-03 09:22:21 +00:00
Oli Scherer
805f389da5 Remove LintExpectationId from Level variants 2025-04-03 09:22:21 +00:00
Oli Scherer
c51816ee59 Make LevelAndSource a struct 2025-04-03 09:17:55 +00:00
Takayuki Maeda
bda2ea4d01
Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar
Move methods from `Map` to `TyCtxt`, part 5.

This eliminates all methods on `Map`. Actually removing `Map` will occur in a follow-up PR.

A follow-up to #137504.

r? `@Zalathar`
2025-04-02 22:52:46 +09:00
bors
ae9173d7dd Auto merge of #139018 - oli-obk:incremental-trait-impls, r=compiler-errors
Various local trait item iteration cleanups

Adding a trait impl for `Foo` unconditionally affected all queries that are interested in a completely independent trait `Bar`. Perf has no effect on this. We probably don't have a good perf test for this tho.

r? `@compiler-errors`

I am unsure about 9d05efb66f as it doesn't improve anything wrt incremental, because we still do all the checks for valid `Drop` impls, which subsequently will still invoke many queries and basically keep the depgraph the same.

I want to do

9549077a47/compiler/rustc_middle/src/ty/trait_def.rs (L141)

but would leave that to a follow-up PR, this one changes enough things as it is
2025-04-02 10:10:50 +00:00
Oli Scherer
49c74d29fd Only walk local items instead of filtering for them later 2025-04-02 07:30:11 +00:00
Nicholas Nethercote
1830245a22 Remove recursion_limit increases.
These are no longer needed now that `Nonterminal` is gone.
2025-04-02 16:25:27 +11:00
Nicholas Nethercote
6713f34ee4 Move methods from Map to TyCtxt, part 5.
This eliminates all methods on `Map`. Actually removing `Map` will occur
in a follow-up PR.
2025-04-02 10:00:46 +11:00
bors
ed201574c5 Auto merge of #138740 - nnethercote:ast-ItemKind-idents, r=fmease
Move `ast::Item::ident` into `ast::ItemKind`

The follow-up to #138384, which did the same thing for `hir::ItemKind`.

r? `@fmease`
2025-04-01 07:21:28 +00:00
Nicholas Nethercote
df247968f2 Move ast::Item::ident into ast::ItemKind.
`ast::Item` has an `ident` field.

- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
  `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
  `Trait`, `TraitAlias`, `MacroDef`, `Delegation`.

- It's always empty for these item kinds: `Use`, `ForeignMod`,
  `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.

There is a similar story for `AssocItemKind` and `ForeignItemKind`.

Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.

The commit is large but it's mostly obvious plumbing work. Some notable
things.

- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
  fields within some of the `ast::ItemKind` variants (specifically:
  `Struct`, `Union`, `Enum`). I might do that in a follow-up; this
  commit is big enough already.

- For the visitors: `FnKind` no longer needs an `ident` field because
  the `Fn` within how has one.

- In the parser, the `ItemInfo` typedef is no longer needed. It was used
  in various places to return an `Ident` alongside an `ItemKind`, but
  now the `Ident` (if present) is within the `ItemKind`.

- In a few places I renamed identifier variables called `name` (or
  `foo_name`) as `ident` (or `foo_ident`), to better match the type, and
  because `name` is normally used for `Symbol`s. It's confusing to see
  something like `foo_name.name`.
2025-04-01 14:08:57 +11:00
Nicholas Nethercote
929749d801 Improve is_doc_keyword.
This is part of the implementation of `#[doc(keyword = "match")]`
attributes used by `std` to provide documentation for keywords.

`is_doc_keyword` currently does a crude keyword range test that's
intended to catch all keywords but misses `kw::Yeet`. This commit
changes it to use `Symbol` methods, including the new `is_weak` method
(required for `union`). `Symbol` methods are much less prone to falling
out of date if new keywords are added.
2025-04-01 07:34:23 +11:00
Nicholas Nethercote
9fb0defa52 Tweak check_doc_keyword.
To use one `kw::Empty` instead of two. It's a little more direct this
way, and avoids `kw::Empty` being used for both "no string" and "empty
string".
2025-04-01 07:34:23 +11:00
Matthias Krüger
ac05597cd7
Rollup merge of #138842 - Noratrieb:inline-exported, r=me,saethlin
Emit `unused_attributes` for `#[inline]` on exported functions

I saw someone post a code sample that contained these two attributes, which immediately made me suspicious.
My suspicions were confirmed when I did a small test and checked the compiler source code to confirm that in these cases, `#[inline]` is indeed ignored (because you can't exactly `LocalCopy`an unmangled symbol since that would lead to duplicate symbols, and doing a mix of an unmangled `GloballyShared` and mangled `LocalCopy` instantiation is too complicated for our current instatiation mode logic, which I don't want to change right now).

So instead, emit the usual unused attribute lint with a message saying that the attribute is ignored in this position.

I think this is not 100% true, since I expect LLVM `inlinehint` to still be applied to such a function, but that's not why people use this attribute, they use it for the `LocalCopy` instantiation mode, where it doesn't work.

r? saethlin as the instantiation guy

Procedurally, I think this should be fine to merge without any lang involvement, as this only does a very minor extension to an existing lint.
2025-03-31 14:36:22 +02:00
Yotam Ofek
9ef35ddc0c use slice::contains where applicable 2025-03-28 12:21:21 +00:00
Nicholas Nethercote
909f449247 Remove kw::Extra checks that are no longer necessary.
Thanks to the introduction of `PatKind::Missing`.
2025-03-28 09:20:40 +11:00
Nicholas Nethercote
9f089e080c Add {ast,hir,thir}::PatKind::Missing variants.
"Missing" patterns are possible in bare fn types (`fn f(u32)`) and
similar places. Currently these are represented in the AST with
`ast::PatKind::Ident` with no `by_ref`, no `mut`, an empty ident, and no
sub-pattern. This flows through to `{hir,thir}::PatKind::Binding` for
HIR and THIR.

This is a bit nasty. It's very non-obvious, and easy to forget to check
for the exceptional empty identifier case.

This commit adds a new variant, `PatKind::Missing`, to do it properly.

The process I followed:
- Add a `Missing` variant to `{ast,hir,thir}::PatKind`.
- Chang `parse_param_general` to produce `ast::PatKind::Missing`
  instead of `ast::PatKind::Missing`.
- Look through `kw::Empty` occurrences to find functions where an
  existing empty ident check needs replacing with a `PatKind::Missing`
  check: `print_param`, `check_trait_item`, `is_named_param`.
- Add a `PatKind::Missing => unreachable!(),` arm to every exhaustive
  match identified by the compiler.
- Find which arms are actually reachable by running the test suite,
  changing them to something appropriate, usually by looking at what
  would happen to a `PatKind::Ident`/`PatKind::Binding` with no ref, no
  `mut`, an empty ident, and no subpattern.

Quite a few of the `unreachable!()` arms were never reached. This makes
sense because `PatKind::Missing` can't happen in every pattern, only
in places like bare fn tys and trait fn decls.

I also tried an alternative approach: modifying `ast::Param::pat` to
hold an `Option<P<Pat>>` instead of a `P<Pat>`, but that quickly turned
into a very large and painful change. Adding `PatKind::Missing` is much
easier.
2025-03-28 09:18:57 +11:00
Vadim Petrochenkov
92d802eda6 expand: Leave traces when expanding cfg attributes 2025-03-26 15:30:12 +03:00
Nicholas Nethercote
e576d8850d Use Option<Symbol> in DuplicateLangItem.
For the the symbols that might not be present, instead of `kw::Empty`.
2025-03-25 16:20:43 +11:00
Noratrieb
1aed58ceb6 Emit unused_attributes for #[inline] on exported functions
I saw someone post a code sample that contained these two attributes,
which immediately made me suspicious.
My suspicions were confirmed when I did a small test and checked the
compiler source code to confirm that in these cases, `#[inline]` is
indeed ignored (because you can't exactly `LocalCopy`an unmangled symbol
since that would lead to duplicate symbols, and doing a mix of an
unmangled `GloballyShared` and mangled `LocalCopy` instantiation is too
complicated for our current instatiation mode logic, which I don't want
to change right now).

So instead, emit the usual unused attribute lint with a message saying
that the attribute is ignored in this position.

I think this is not 100% true, since I expect LLVM `inlinehint` to still
be applied to such a function, but that's not why people use this
attribute, they use it for the `LocalCopy` instantiation mode, where it
doesn't work.
2025-03-24 20:07:35 +01:00
Matthias Krüger
4457da3dc4
Rollup merge of #138786 - bjorn3:driver_code_move, r=compiler-errors
Move some driver code around

`--emit mir`, `#[rustc_symbol_name]` and `#[rustc_def_path]` now run before codegen and thus work even if codegen fails. This can help with debugging.
2025-03-22 21:34:39 +01:00
Matthias Krüger
c354a97bd9
Rollup merge of #138570 - folkertdev:naked-function-target-feature-gate, r=Amanieu
add `naked_functions_target_feature` unstable feature

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

tagging https://github.com/rust-lang/rust/pull/134213 https://github.com/rust-lang/rust/issues/90957

This PR puts `#[target_feature(/* ... */)]` on `#[naked]` functions behind its own feature gate, so that naked functions can be stabilized. It turns out that supporting `target_feature` on naked functions is tricky on some targets, so we're splitting it out to not block stabilization of naked functions themselves. See the tracking issue for more information and workarounds.

Note that at the time of writing, the `target_features` attribute is ignored when generating code for naked functions.

r? ``@Amanieu``
2025-03-21 15:48:52 +01:00
bjorn3
cd929bfccb Fix lint name in unused linker_messages warning 2025-03-21 13:59:29 +00:00
Matthias Krüger
1135a63286
Rollup merge of #138724 - fmease:list-stems-bear-no-name, r=nnethercote
Check attrs: Don't try to retrieve the name of list stems

Fixes #138723.

r? nnethercote or compiler
2025-03-21 06:56:47 +01:00
bors
78948ac259 Auto merge of #138515 - petrochenkov:cfgtrace, r=nnethercote
expand: Leave traces when expanding `cfg_attr` attributes

Currently `cfg_trace` just disappears during expansion, but after this PR `#[cfg_attr(some tokens)]` will leave a `#[cfg_attr_trace(some tokens)]` attribute instead of itself in AST after expansion (the new attribute is built-in and inert, its inner tokens are the same as in the original attribute).
This trace attribute can then be used by lints or other diagnostics, #133823 has some examples.

Tokens in these trace attributes are set to an empty token stream, so the traces are non-existent for proc macros and cannot affect any user-observable behavior.
This is also a weakness, because if a proc macro processes some code with the trace attributes, they will be lost, so the traces are best effort rather than precise.

The next step is to do the same thing with `cfg` attributes (`#[cfg(TRUE)]` currently remains in both AST and tokens after expanding, it should be replaced with a trace instead).

The idea belongs to `@estebank.`
2025-03-20 19:24:48 +00:00
León Orell Valerian Liehr
b5069da9df
Check attrs: Don't try to retrieve the name of list stems 2025-03-19 23:29:35 +01:00
bors
259fdb5212 Auto merge of #138630 - matthiaskrgr:rollup-kk1gogr, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #138384 (Move `hir::Item::ident` into `hir::ItemKind`.)
 - #138508 (Clarify "owned data" in E0515.md)
 - #138531 (Store test diffs in job summaries and improve analysis formatting)
 - #138533 (Only use `DIST_TRY_BUILD` for try jobs that were not selected explicitly)
 - #138556 (Fix ICE: attempted to remap an already remapped filename)
 - #138608 (rustc_target: Add target feature constraints for LoongArch)
 - #138619 (Flatten `if`s in `rustc_codegen_ssa`)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-03-18 05:58:46 +00:00
Matthias Krüger
e1acc68c9d
Rollup merge of #138384 - nnethercote:hir-ItemKind-idents, r=fmease
Move `hir::Item::ident` into `hir::ItemKind`.

 `hir::Item` has an `ident` field.

- It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, Trait`, TraitAalis`.

- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`, `Impl`.

- For `Use`, it is non-empty for `UseKind::Single` and empty for `UseKind::{Glob,ListStem}`.

All of this is quite non-obvious; the only documentation is a single comment saying "The name might be a dummy name in case of anonymous items". Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out.

This is step towards `kw::Empty` elimination (#137978).

r? `@fmease`
2025-03-17 22:49:04 +01:00
Nicholas Nethercote
f2ddbcd24b Move hir::Item::ident into hir::ItemKind.
`hir::Item` has an `ident` field.

- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
  `Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
  Trait`, TraitAalis`.

- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`,
  `Impl`.

- For `Use`, it is non-empty for `UseKind::Single` and empty for
  `UseKind::{Glob,ListStem}`.

All of this is quite non-obvious; the only documentation is a single
comment saying "The name might be a dummy name in case of anonymous
items". Some sites that handle items check for an empty ident, some
don't. This is a very C-like way of doing things, but this is Rust, we
have sum types, we can do this properly and never forget to check for
the exceptional case and never YOLO possibly empty identifiers (or
possibly dummy spans) around and hope that things will work out.

The commit is large but it's mostly obvious plumbing work. Some notable
things.

- A similar transformation makes sense for `ast::Item`, but this is
  already a big change. That can be done later.

- Lots of assertions are added to item lowering to ensure that
  identifiers are empty/non-empty as expected. These will be removable
  when `ast::Item` is done later.

- `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does.

- `lower_use_tree` is significantly simpler. No more confusing `&mut
  Ident` to deal with.

- `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's
  used with `unwrap` in a few places; sometimes it's hard to tell
  exactly which item kinds might occur. None of these unwraps fail on
  the test suite. It's conceivable that some might fail on alternative
  input. We can deal with those if/when they happen.

- In `trait_path` the `find_map`/`if let` is replaced with a loop, and
  things end up much clearer that way.

- `named_span` no longer checks for an empty name; instead the call site
  now checks for a missing identifier if necessary.

- `maybe_inline_local` doesn't need the `glob` argument, it can be
  computed in-function from the `renamed` argument.

- `arbitrary_source_item_ordering::check_mod` had a big `if` statement
  that was just getting the ident from the item kinds that had one. It
  could be mostly replaced by a single call to the new `ItemKind::ident`
  method.

- `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size,
  and that's what matters, because `ItemKind` only occurs within `Item`.
2025-03-18 06:29:50 +11:00
bjorn3
60b785fc8d Mark #[rustc_std_internal_symbol] as extern indicator
It currently implies #[no_mangle] which is alread an extern indicator,
but this will change in a future commit.
2025-03-17 14:06:56 +00:00
bjorn3
98b9d0232f Allow #[rustc_std_internal_symbol] on foreign items 2025-03-17 14:06:56 +00:00
Vadim Petrochenkov
9dd4e4cad1 expand: Leave traces when expanding cfg_attr attributes 2025-03-17 15:58:25 +03:00
Yotam Ofek
51e8309f50 Flatten and simplify some control flow 2025-03-17 09:15:49 +00:00