Commit Graph

998 Commits

Author SHA1 Message Date
Matthias Krüger
c896f06bdb
Rollup merge of #130314 - compiler-errors:mac-prec, r=davidtwco
Use the same precedence for all macro-like exprs

No need to make these have a different precedence since they're all written like `whatever!(expr)`, and it makes it simpler when adding new macro-based built-in operators in the future.
2024-09-17 17:28:33 +02:00
Michael Goulet
c16a90479a Use the same precedence for all macro-like exprs 2024-09-13 12:53:30 -04:00
Noah Lev
8b75004bca Fix anon const def-creation when macros are involved
Ever since #125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s,
which don't have associated `DefId`s. To deal with the fact that we don't have
resolution information in `DefCollector`, we decided to implement a process
where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we
would avoid creating a def for it in `DefCollector`. If later, in AST lowering,
we realized it turned out to be a unit struct literal, or we were lowering it
to something that didn't use `hir::ConstArg`, we'd create its def there.

However, let's say we have a macro `m!()` that expands to a reference to a free
constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`),
then in def collection, it appears to be a nontrivial anon const and we create
a def. But the macro expands to something that looks like a trivial const arg,
but is not, so in AST lowering we "fix" the mistake we assumed def collection
made and create a def for it. This causes a duplicate definition ICE.

The ideal long-term fix for this is a bit unclear. One option is to delay def
creation for all expression-like nodes until AST lowering (see #128844 for an
incomplete attempt at this). This would avoid issues like this one that are
caused by hacky workarounds. However, this approach has some downsides as well,
and the best approach is yet to be determined.

In the meantime, this PR fixes the bug by delaying def creation for anon consts
whose bodies are macro invocations until after we expand the macro and know
what is inside it. This is accomplished by adding information to create the
anon const's def to the data in `Resolver.invocation_parents`.
2024-09-12 13:48:30 -04:00
Stuart Cook
a3d9d13d7a
Rollup merge of #130252 - compiler-errors:const-gen, r=chenyukang
Properly report error on `const gen fn`

Fixes #130232

Also removes some (what I thought were unused) functions, and fixes a bug in clippy where we considered `gen fn` to be the same as `fn` because it was only built to consider asyncness.
2024-09-12 20:37:18 +10:00
Stuart Cook
57020e0f8c
Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu
Fix `clippy::useless_conversion`

Self-explanatory. Probably the last clippy change I'll actually put up since this is the only other one I've actually seen in the wild.
2024-09-12 20:37:17 +10:00
Stuart Cook
3ba12756d3
Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister
Simplify some nested `if` statements

Applies some but not all instances of `clippy::collapsible_if`. Some ended up looking worse afterwards, though, so I left those out. Also applies instances of `clippy::collapsible_else_if`

Review with whitespace disabled please.
2024-09-12 20:37:16 +10:00
Michael Goulet
8dc227866f Remove unused functions from ast CoroutineKind 2024-09-11 19:24:40 -04:00
Jubilee
a31a8fe0cf
Rollup merge of #130114 - eduardosm:needless-returns, r=compiler-errors
Remove needless returns detected by clippy in the compiler
2024-09-11 15:53:22 -07:00
Michael Goulet
594de02cba Properly deny const gen/async gen fns 2024-09-11 18:39:06 -04:00
Michael Goulet
6d064295c8 clippy::useless_conversion 2024-09-11 17:52:53 -04:00
Michael Goulet
af8d911d63 Also fix if in else 2024-09-11 17:24:01 -04:00
Folkert de Vries
6ca5ec7b4e disallow naked_asm! outside of #[naked] functions 2024-09-10 15:19:14 +02:00
Eduardo Sánchez Muñoz
0b20ffcb63 Remove needless returns detected by clippy in the compiler 2024-09-09 13:32:22 +02:00
Michael Goulet
97910580aa Add initial support for raw lifetimes 2024-09-06 10:32:48 -04:00
Camille GILLOT
f68f66538a Create opaque definitions in resolver. 2024-08-31 20:14:43 +00:00
Matthias Krüger
1fd0c71818
Rollup merge of #120221 - compiler-errors:statements-are-not-patterns, r=nnethercote
Don't make statement nonterminals match pattern nonterminals

Right now, the heuristic we use to check if a token may begin a pattern nonterminal falls back to `may_be_ident`:
ef71f1047e/compiler/rustc_parse/src/parser/nonterminal.rs (L21-L37)

This has the unfortunate side effect that a `stmt` nonterminal eagerly matches against a `pat` nonterminal, leading to a parse error:
```rust
macro_rules! m {
    ($pat:pat) => {};
    ($stmt:stmt) => {};
}

macro_rules! m2 {
    ($stmt:stmt) => {
        m! { $stmt }
    };
}

m2! { let x = 1 }
```

This PR fixes it by more accurately reflecting the set of nonterminals that may begin a pattern nonterminal.

As a side-effect, I modified `Token::can_begin_pattern` to work correctly and used that in `Parser::nonterminal_may_begin_with`.
2024-08-31 10:08:51 +02:00
Matthias Krüger
110c3df7fd
Rollup merge of #126013 - nnethercote:unreachable_pub, r=Urgau
Add `#[warn(unreachable_pub)]` to a bunch of compiler crates

By default `unreachable_pub` identifies things that need not be `pub` and tells you to make them `pub(crate)`. But sometimes those things don't need any kind of visibility. So they way I did these was to remove the visibility entirely for each thing the lint identifies, and then add `pub(crate)` back in everywhere the compiler said it was necessary. (Or occasionally `pub(super)` when context suggested that was appropriate.) Tedious, but results in more `pub` removal.

There are plenty more crates to do but this seems like enough for a first PR.

r? `@compiler-errors`
2024-08-27 00:41:57 +02:00
Michael Goulet
c61f85b6dd Don't make pattern nonterminals match statement nonterminals 2024-08-26 18:30:15 -04:00
Trevor Gross
dfe7d5c31e
Rollup merge of #128524 - chenyukang:yukang-fix-127930-invalid-outer-style-sugg, r=cjgillot
Don't suggest turning crate-level attributes into outer style

Fixes #127930
2024-08-24 21:03:31 -05:00
Nicholas Nethercote
194489473d Add warn(unreachable_pub) to several crates.
It requires no additonal changes to these crates, but will prevent
unnecessary `pub`s in the future.
2024-08-16 08:46:13 +10:00
León Orell Valerian Liehr
c4c518d2d4
Use more slice patterns inside the compiler 2024-08-07 13:37:52 +02:00
yukang
22aa104bce don't suggest turning crate-level attributes into outer style 2024-08-04 00:11:16 +08: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
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
d3858f7465
improve error message when global_asm! uses asm! options 2024-07-25 22:33:52 +02:00
bors
aa877bc71c Auto merge of #128195 - matthiaskrgr:rollup-195dfdf, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #126908 (Use Cow<'static, str> for InlineAsmTemplatePiece::String)
 - #127999 (Inject arm32 shims into Windows metadata generation)
 - #128137 (CStr: derive PartialEq, Eq; add test for Ord)
 - #128185 (Fix a span error when parsing a wrong param of function.)
 - #128187 (Fix 1.80.0 version in RELEASES.md)
 - #128189 (Turn an unreachable code path into an ICE)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-07-25 18:05:00 +00: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
GnomedDev
db8cdc5d37
Use Cow<'static, str> for InlineAsmTemplatePiece::String 2024-07-24 21:11:55 +01:00
Folkert
c31ff97bf1
centralize turning asm flags into human readable names 2024-07-24 15:27:18 +02:00
Oli Scherer
e9f32d0ca6 Avoid passing state that will not be visited 2024-07-22 14:34:45 +00:00
Oli Scherer
91b26fcb89 Update trait name from Noop -> Walk 2024-07-22 14:02:16 +00:00
Oli Scherer
8d290058c9 Always pass the visitor as the first argument to walk* functions 2024-07-22 14:01:24 +00:00
Oli Scherer
754bdef793 Sync mut_visit function names with immut visit ones (s/noop_visit/walk/) 2024-07-22 14:01:24 +00:00
Oli Scherer
6f85f20520 Add Ident to FnKind::Fn, just like with the immutable visitor 2024-07-22 14:01:23 +00:00
Oli Scherer
e426f262fd Split up visit_path so MutVisitor has a path_segment method just like the immutable visitor 2024-07-22 14:01:23 +00:00
Oli Scherer
545553ca4f Pass id and span to visit_fn, just like for the immutable visitor 2024-07-22 14:01:23 +00:00
Oli Scherer
1b9ac0011f Make function items in mut visitors all go through the same visit_fn function, just like with immutable visitors 2024-07-22 14:01:23 +00:00
Oli Scherer
c064b363b9 Track visit_param_bound in mut visit just like in the immutable visitor 2024-07-22 14:01:23 +00:00
Oli Scherer
5241d8bb19 Merge impl and trait item mut visitor methods to mirror immut visitor 2024-07-22 14:01:23 +00:00
Trevor Gross
fa1303662a
Rollup merge of #127806 - nnethercote:parser-improvements, r=spastorino
Some parser improvements

I was looking closely at attribute handling in the parser while debugging some issues relating to #124141, and found a few small improvements.

``@spastorino``
2024-07-17 19:53:27 -05:00
Oli Scherer
117ff0a4fd Fix a bunch of sites that were walking instead of visiting, making it impossible for visitor impls to look at these values 2024-07-16 15:50:36 +00:00
Nicholas Nethercote
9c4f3dbd06 Remove references to maybe_whole_expr.
It was removed in #126571.
2024-07-16 16:40:35 +10:00
Jubilee
125343e7ab
Rollup merge of #127558 - nnethercote:more-Attribute-cleanups, r=petrochenkov
More attribute cleanups

A follow-up to #127308.

r? ```@petrochenkov```
2024-07-13 20:19:46 -07:00
Oli Scherer
3562ec74ca Make visit_clobber's impl safe 2024-07-10 07:54:17 +00:00
Nicholas Nethercote
478ba59026 Add some comments.
Explaining things that took me some time to work out.
2024-07-10 17:03:58 +10:00
Nicholas Nethercote
d6ebbbfcb2 Factor out AttrsTarget flattening code.
This commit does the following.
- Pulls the code out of `AttrTokenStream::to_token_trees` into a new
  function `attrs_and_tokens_to_token_trees`.
- Simplifies `TokenStream::from_ast` by calling the new function. This
  is nicer than the old way, which created a temporary
  `AttrTokenStream` containing a single `AttrsTarget` (which required
  some cloning) just to call `to_token_trees` on it. (It is good to
  remove this use of `AttrsTarget` which isn't related to `cfg_attr`
  expansion.)
2024-07-10 17:03:17 +10:00
Nicholas Nethercote
fee152556f Rework Attribute::get_tokens.
Returning `Vec<TokenTree>` works better for the call sites than
returning `TokenStream`.
2024-07-10 14:51:41 +10:00
Matthias Krüger
510020ad4c
Rollup merge of #127308 - nnethercote:Attribute-cleanups, r=petrochenkov
Attribute cleanups

More refactoring done while trying to fix the final remaining test failure for #124141.

r? `@petrochenkov`
2024-07-07 14:22:01 +02:00
Nicholas Nethercote
9f16f1f6f6 Add an size assertion.
`Option<LazyAttrTokenStream>` is the type that's actually used in all
the aST nodes.
2024-07-07 16:25:22 +10:00
Nicholas Nethercote
3a5c4b6e4e Rename some attribute types for consistency.
- `AttributesData` -> `AttrsTarget`
- `AttrTokenTree::Attributes` -> `AttrTokenTree::AttrsTarget`
- `FlatToken::AttrTarget` -> `FlatToken::AttrsTarget`
2024-07-07 16:14:30 +10:00