Commit graph

151 commits

Author SHA1 Message Date
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
7e1f2f9c54 Augment some tests involving attributes.
This shows places where the use of `name_or_empty` causes problems, i.e.
we print empty identifiers in error messages:
```
error: unrecognized field name ``
error: `` isn't a valid `#[macro_export]` argument
`#[no_sanitize()]` should be applied to a function
```
(The last one is about an attribute `#[no_sanitize("address")]`.)

The next commit will fix these.
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
Nicholas Nethercote
4be670f89b Warnings-as-errors in check-builtin-attr-ice.rs.
This adds two new warnings, both of which print the attribute
incorrectly as `#[]`. The next commit will fix this.
2025-04-17 09:50:52 +10:00
jyn
d50a8d5fb3 Improve -Z crate-attr diagnostics
- Show the `#![ ... ]` in the span (to make it clear that it should not
  be included in the CLI argument)
- Show more detailed errors when the crate has valid token trees but
  invalid syntax.
  Previously, `crate-attr=feature(foo),feature(bar)` would just say
  "invalid crate attribute" and point at the comma. Now, it explicitly
  says that the comma was unexpected, which is useful when using
  `--error-format=short`. It also fixes the column to show the correct
  span.
- Recover from parse errors. Previously we would abort immediately on
  syntax errors; now we go on to try and type-check the rest of the
  crate.

The new diagnostic code also happens to be slightly shorter.
2025-04-13 16:46:02 -04:00
Vadim Petrochenkov
b3f75353a2 UI tests: add missing diagnostic kinds where possible 2025-04-08 23:06:31 +03:00
bors
c6c179662d Auto merge of #133781 - cjgillot:shallow-allowed-lints, r=petrochenkov
Do not visit whole crate to compute `lints_that_dont_need_to_run`.

This allows to reuse the computed lint levels instead of re-visiting the whole crate.
2025-04-07 21:03:55 +00:00
Guillaume Gomez
b1d67b2492
Rollup merge of #139391 - TaKO8Ki:check-if-merged-attrs-list-is-empty, r=jdonszelmann
Check if merged attributes list is empty in expr

Fixes #139373

In the example code, an [`UnrecognizedReprHint`](6b5ccfc87f/compiler/rustc_attr_parsing/src/attributes/repr.rs (L155)) error is output, and the list of merged attributes becomes empty. This causes a [panic](6b5ccfc87f/compiler/rustc_ast_lowering/src/lib.rs (L618)) to occur. So, it's necessary to check if merged attributes list is empty as other functions do.

ref: 6b5ccfc87f/compiler/rustc_ast_lowering/src/lib.rs (L896)
2025-04-06 18:08:11 +02:00
Takayuki Maeda
6b5ccfc87f check if merged attributes list is empty 2025-04-05 06:09:14 +09:00
Camille GILLOT
280a1d8edb Do not visit whole crate to compute lints_that_dont_need_to_run. 2025-04-04 12:37:38 +00:00
Vadim Petrochenkov
4d64990690 compiletest: Require //~ annotations even if error-pattern is specified 2025-04-03 11:08:55 +03:00
Nicholas Nethercote
592d113ff2 Fix problem causing rusqlite compilation to OOM.
This makes the expression re-parsing more like how it's originally done
in `parse_nonterminal`.
2025-04-02 06:21:18 +11:00
Nicholas Nethercote
81afdbc161 Fix a problem with metavars and inner attributes. 2025-04-02 06:21:18 +11:00
Nicholas Nethercote
49ed25b5d2 Remove NtExpr and NtLiteral.
Notes about tests:
- tests/ui/rfcs/rfc-2294-if-let-guard/feature-gate.rs: some messages are
  now duplicated due to repeated parsing.

- tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs: ditto.

- `tests/ui/proc-macro/macro-rules-derive-cfg.rs`: the diff looks large
  but the only difference is the insertion of a single
  invisible-delimited group around a metavar.

- `tests/ui/attributes/nonterminal-expansion.rs`: a slight span
  degradation, somehow related to the recent massive attr parsing
  rewrite (#135726). I couldn't work out exactly what is going wrong,
  but I don't think it's worth holding things up for a single slightly
  suboptimal error message.
2025-04-02 06:20:35 +11:00
Nicholas Nethercote
1a6266340e Add a test with an empty crate name.
This error was untested.
2025-03-25 16:14:27 +11:00
Jeff Martin
660509d717 Fix the "used_with_archive" test on Fuchsia
This change adds Fuchsia OS as a target to the cfg_attr in the
pre_main_constructor external declaration. This allows the
"tests/ui/attributes/used_with_archive.rs" to pass against Fuchsia.
2025-03-20 12:25:08 -04:00
许杰友 Jieyou Xu (Joe)
7c1555cb9b
Rollup merge of #138471 - spencer3035:move-ui-test-1ofn, r=jieyouxu
Clean up some tests in tests/ui

I cleaned up 3 top level tests, keeping the changes minor because it is my first PR and wanted to get feedback before doing more changes/PRs.

Tracking issues:
https://github.com/rust-lang/rust/issues/73494
https://github.com/rust-lang/rust/issues/133895

r? jieyouxu
2025-03-16 09:40:08 +08:00
Spencer
9f06585c0e improves outer mod attribute test 2025-03-15 10:52:07 -06:00
Esteban Küber
f0b8e13b59 Do not suggest using -Zmacro-backtrace for builtin macros
For macros that are implemented on the compiler, we do *not* mention the `-Zmacro-backtrace` flag. This includes `derive`s and standard macros.
2025-03-14 19:50:03 +00:00
bors
6650252439 Auto merge of #128440 - oli-obk:defines, r=lcnr
Add `#[define_opaques]` attribute and require it for all type-alias-impl-trait sites that register a hidden type

Instead of relying on the signature of items to decide whether they are constraining an opaque type, the opaque types that the item constrains must be explicitly listed.

A previous version of this PR used an actual attribute, but had to keep the resolved `DefId`s in a side table.

Now we just lower to fields in the AST that have no surface syntax, instead a builtin attribute macro fills in those fields where applicable.

Note that for convenience referencing opaque types in associated types from associated methods on the same impl will not require an attribute. If that causes problems `#[defines()]` can be used to overwrite the default of searching for opaques in the signature.

One wart of this design is that closures and static items do not have generics. So since I stored the opaques in the generics of functions, consts and methods, I would need to add a custom field to closures and statics to track this information. During a T-types discussion we decided to just not do this for now.

fixes #131298
2025-03-11 18:13:31 +00:00
Oli Scherer
cb4751d4b8 Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
jyn
512ebed59a add more -Z crate-attr tests 2025-03-11 00:13:17 -04:00
Michael Goulet
34d273b4bf
Rollup merge of #137758 - jdonszelmann:fix-137662, r=nnethercote
fix usage of ty decl macro fragments in attributes

See the test case. Due to one missing code path (and also the changes in #137517), using $ty or other specific fragments as part of an attr wouldn't work. $tt used to work since it wouldn't be parsed anywhere along the way.

Closes #137662
2025-03-06 12:22:15 -05:00
Matthias Krüger
65c34bb477
Rollup merge of #137794 - tshepang:make-qnx-pass, r=pietroalbini
make qnx pass a test

[tests/ui/attributes/used_with_archive.rs](b202430084/tests/ui/attributes/used_with_archive.rs) fails when executed for QNX targets, because its stdout does not match [this content](b202430084/tests/ui/attributes/used_with_archive.run.stdout)
2025-03-03 06:41:35 +01:00
Tshepang Mbambo
f8091c49ff qnx: avoid test failure
Fixes tests/ui/attributes/used_with_archive.rs fail
2025-02-28 16:10:41 +02:00
Tshepang Mbambo
e66bf08f93 sort list 2025-02-28 16:10:38 +02:00
Jana Dönszelmann
41dd80aeaa
add test to reproduce #137662 (using ty decl macro fragment in an attr) and fix it 2025-02-28 09:43:46 +01:00
Nicholas Nethercote
7ea59e053b Remove NtMeta.
Note: there was an existing code path involving `Interpolated` in
`MetaItem::from_tokens` that was dead. This commit transfers that to the
new form, but puts an `unreachable!` call inside it.
2025-02-28 08:42:06 +11:00
León Orell Valerian Liehr
1bb43192a2
Rollup merge of #137622 - jdonszelmann:fix-137589, r=compiler-errors
fix attribute-related ICE when parsing macro on the rhs of a name-value attribute

r? ``@oli-obk``

Closes: #137589
2025-02-26 04:15:09 +01:00
Jana Dönszelmann
4bf66c57fa
fix #137589 2025-02-25 18:17:32 +01:00
bors
c51b9b6d52 Auto merge of #133832 - madsmtm:apple-symbols.o, r=DianQK
Make `#[used]` work when linking with `ld64`

To make `#[used]` work in static libraries, we use the `symbols.o` trick introduced in https://github.com/rust-lang/rust/pull/95604.

However, the linker shipped with Xcode, ld64, works a bit differently from other linkers; in particular, [it completely ignores undefined symbols by themselves](https://github.com/apple-oss-distributions/ld64/blob/ld64-954.16/src/ld/parsers/macho_relocatable_file.cpp#L2455-L2468), and only consider them if they have relocations (something something atoms something fixups, I don't know the details).

So to make the `symbols.o` file work on ld64, we need to actually insert a relocation. That's kinda cumbersome to do though, since the relocation must be valid, and hence must point to a valid piece of machine code, and is hence very architecture-specific.

Fixes https://github.com/rust-lang/rust/issues/133491, see that for investigation.

---

Another option would be to pass `-u _foo` to the final linker invocation. This has the problem that `-u` causes the linker to not be able to dead-strip the symbol, which is undesirable. (If we did this, we would possibly also want to do it by putting the arguments in a file by itself, and passing that file via ``@`,` e.g. ``@undefined_symbols.txt`,` similar to https://github.com/rust-lang/rust/issues/52699, though that [is only supported since Xcode 12](https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes#Linking), and I'm not sure we wanna bump that).

Various other options that are probably all undesirable as they affect link time performance:
- Pass `-all_load` to the linker.
- Pass `-ObjC` to the linker (the Objective-C support in the linker has different code paths that load more of the binary), and instrument the binaries that contain `#[used]` symbols.
- Pass `-force_load` to libraries that contain `#[used]` symbols.

Failed attempt: Embed `-u _foo` in the object file with `LC_LINKER_OPTION`, akin to https://github.com/rust-lang/rust/issues/121293. Doesn't work, both because `ld64` doesn't read that from archive members unless it already has a reason to load the member (which is what this PR is trying to make it do), and because `ld64` only support the `-l`, `-needed-l`, `-framework` and `-needed_framework` flags in there.

---

TODO:
- [x] Support all Apple architectures.
- [x] Ensure that this works regardless of the actual type of the symbol.
- [x] Write up more docs.
- [x] Wire up a few proper tests.

`@rustbot` label O-apple
2025-02-25 11:59:11 +00:00
Jana Dönszelmann
2f0652745d
add test to verify that #132391 can be closed 2025-02-24 14:31:19 +01:00
Jana Dönszelmann
309b46ad68
Fix test output expectations 2025-02-24 14:31:19 +01:00
Mads Marquart
b202430084 Make #[used] work when linking with ld64 2025-02-24 04:04:59 +01:00
Esteban Küber
835d434c79 Reword message 2025-02-20 17:55:31 +00:00
Esteban Küber
fe7ed278b7 Specify scope in out_of_scope_macro_calls lint
```
warning: cannot find macro `in_root` in the crate root
  --> $DIR/key-value-expansion-scope.rs:1:10
   |
LL | #![doc = in_root!()]
   |          ^^^^^^^ not found in the crate root
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
   = help: import `macro_rules` with `use` to make it callable above its definition
   = note: `#[warn(out_of_scope_macro_calls)]` on by default
```
2025-02-19 18:29:00 +00:00
León Orell Valerian Liehr
9b6fd35738
Reject macro calls inside of #![crate_name] 2025-02-15 16:47:30 +01:00
León Orell Valerian Liehr
46d53a68aa
Move #![crate_type] UI tests into attributes directory
Gets rid of two top-level UI tests which is always great.

Furthermore, move `need-crate-arg-ignore-tidy$x.rs`
from `command/` to `invalid-compile-flags/`.
`command/` concerns `std::process::Command` tests, not CLI tests.
2025-02-15 03:41:07 +01:00
Michael Goulet
6d71251cf9 Trim suggestion parts to the subset that is purely additive 2025-02-14 00:44:10 -08:00
Michael Goulet
b480a9214a Use underline suggestions for purely 'additive' replacements 2025-02-14 00:27:13 -08:00
Esteban Küber
f0845adb0c Show diff suggestion format on verbose replacement
```
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
  --> $DIR/attempted-access-non-fatal.rs:7:15
   |
LL |     let _ = 2.l;
   |               ^
   |
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
   |
LL -     let _ = 2.l;
LL +     let _ = 2.0f64;
   |
```
2025-02-10 20:21:39 +00:00
Esteban Küber
dd52bfc76e Reword "crate not found" resolve message
```
error[E0432]: unresolved import `some_novel_crate`
 --> file.rs:1:5
  |
1 | use some_novel_crate::Type;
  |     ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `some_novel_crate`
```

On resolve errors where there might be a missing crate, mention `cargo add foo`:

```
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nope`
  --> $DIR/conflicting-impl-with-err.rs:4:11
   |
LL | impl From<nope::Thing> for Error {
   |           ^^^^ use of unresolved module or unlinked crate `nope`
   |
   = help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
```
2025-01-24 01:19:50 +00:00
Mara Bos
585c9765a2 Update tests. 2025-01-07 16:04:14 +01:00
Ralf Jung
ac9cb908ac turn rustc_box into an intrinsic 2025-01-03 12:01:31 +01:00
Zalathar
835fbcbcab Remove the -test suffix from normalize directives 2024-12-27 19:58:16 +11:00
许杰友 Jieyou Xu (Joe)
7424b898e9 Adjust tests/ui/attr-usage-inline.rs
- Move `tests/ui/attr-usage-inline.rs` to `tests/ui/attributes/inline/`.
- Briefly document test intent.
- Drop unnecessary `#[allow(dead_code)]` as this is allowed-by-default
  for ui test suite.
2024-12-17 19:57:42 +08:00
许杰友 Jieyou Xu (Joe)
88f8bf7e56 Adjust tests/ui/attr-shebang.rs
- Move `tests/ui/attr-shebang.rs` to `tests/ui/attributes/`.
- Downgrade test to `check-pass`, this would fail very early if the
  parser did not accept `#![..]` attributes.
- Briefly document test intent.
2024-12-17 19:22:21 +08:00
许杰友 Jieyou Xu (Joe)
0fc6be7898 Adjust tests/ui/attr-bad-crate-attr.rs
- Move `tests/ui/attr-bad-crate-attr.rs` to `tests/ui/attributes/`.
- Briefly document test intent add link to relevant Reference docs.
2024-12-17 19:18:30 +08:00
Esteban Küber
49a22a4245 Filter empty lines, comments and delimiters from previous to last multiline span rendering 2024-12-12 23:36:27 +00:00
León Orell Valerian Liehr
94d780d647
Rollup merge of #131558 - sassman:feat/warnin-for-no-mangle-together-with-export-name, r=Urgau
Lint on combining `#[no_mangle]` and `#[export_name]`

This is my very first contribution to the compiler, even though I read the [chapter about lints](https://rustc-dev-guide.rust-lang.org/diagnostics.html) I'm not very certain that this ~~new lint is done right as a builtin lint~~ PR is right. I appreciate any guidance on how to improve the code.

- Add test for issue #47446
- ~~Implement the new lint `mixed_export_name_and_no_mangle` as a builtin lint (not sure if that is the right way to go)~~ Extend `unused_attributes` lint
- Add suggestion how to fix it

<details>

<summary>Old proposed new lint</summary>

> The `mixed_export_name_and_no_mangle` lint detects usage of both `#[export_name]` and `#[no_mangle]` on the same item which results on `#[no_mangle]` being ignored.
>
> *warn-by-default*
>
> ### Example
>
> ```rust
> #[no_mangle] // ignored
> #[export_name = "foo"] // takes precedences
> pub fn bar() {}
> ```
>
> ### Explanation
>
> The compiler will not respect the `#[no_mangle]` attribute when generating the symbol name for the function, as the `#[export_name]` attribute takes precedence. This can lead to confusion and is unnecessary.

</details>
2024-12-10 08:55:54 +01:00