Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decl_macro. no field 0 on type. a field with a similar name exists: 0 #116334

Closed
planetoryd opened this issue Oct 2, 2023 · 4 comments · Fixed by #116429
Closed

decl_macro. no field 0 on type. a field with a similar name exists: 0 #116334

planetoryd opened this issue Oct 2, 2023 · 4 comments · Fixed by #116429
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros-2.0 Area: Declarative macros 2.0 (#39412) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-decl_macro `#![feature(decl_macro)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@planetoryd
Copy link

I tried this code:

https://github.com/planetoryd/rust_bug_missing_field

#![feature(decl_macro)]

make_struct!(Uh);

macro make_struct ($id:ident) {
    pub struct $id(pub i32);
}

fn main() {
    let k = Uh(2);
    println!("{}", k.0);
}

The equivalent code without macro works.

hash@hash-pc /s/e/missing_field (master)> cargo b
   Compiling missing_field v0.1.0 (/space/experim/missing_field)
error[E0609]: no field `0` on type `Uh`
  --> src/main.rs:11:22
   |
11 |     println!("{}", k.0);
   |                      ^ help: a field with a similar name exists: `0`

For more information about this error, try `rustc --explain E0609`.
error: could not compile `missing_field` (bin "missing_field") due to previous error
warning: build failed, waiting for other jobs to finish...

Meta

rustc --version --verbose:

rustc 1.75.0-nightly (e0d7ed1f4 2023-10-01)
binary: rustc
commit-hash: e0d7ed1f453fb54578cc96dfea859b0e7be15016
commit-date: 2023-10-01
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: **17.0.2**
@planetoryd planetoryd added the C-bug Category: This is a bug. label Oct 2, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 2, 2023
@planetoryd planetoryd changed the title decl_macro. no field 0 on type. a field with a similar name exists: 0. decl_macro. no field 0 on type. a field with a similar name exists: 0 Oct 2, 2023
@planetoryd
Copy link
Author

Methods don't work either.

@fmease
Copy link
Member

fmease commented Oct 2, 2023

This works as intended, this is macro hygiene at play. The tuple field 0 declared inside the macro is invisible to the outside world.

Typically if you'd like to make such an identifier accessible, you'd turn it into a parameter like $field:ident. However, for tuple structs, I don't actually know if there's a way to do that :/

To elaborate, the (implicit) identifier 0 inside the macro is different from the identifier 0 found in main, they differ by syntactic context. For example, in this playground I seemingly define a struct with two (!) fields field which works because they live in a different context.

@fmease
Copy link
Member

fmease commented Oct 2, 2023

If you're fine with changing your tuple struct to a normal struct, then you can fix your code like this: Playground.

Alternatively, you can turn the macro 2.0 into a macro_rules! macro which follows different hygiene rules.

@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros-2.0 Area: Declarative macros 2.0 (#39412) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Oct 2, 2023
@fmease
Copy link
Member

fmease commented Oct 2, 2023

Let's leave this open since the diagnostic could be improved.

@fmease fmease self-assigned this Oct 2, 2023
@fmease fmease added the F-decl_macro `#![feature(decl_macro)]` label Oct 2, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 5, 2023
…, r=compiler-errors

Diagnostics: Be more careful when suggesting struct fields

Consolidate the various places which filter out struct fields that shouldn't be suggested into a single function.

Previously, each of those code paths had slightly different and incomplete metrics for no good reason. Now, there's only a single 'complete' metric (namely `is_field_suggestable`) which also filters out hygienic fields that come from different syntax contexts.

Fixes rust-lang#116334.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 5, 2023
…, r=compiler-errors

Diagnostics: Be more careful when suggesting struct fields

Consolidate the various places which filter out struct fields that shouldn't be suggested into a single function.

Previously, each of those code paths had slightly different and incomplete metrics for no good reason. Now, there's only a single 'complete' metric (namely `is_field_suggestable`) which also filters out hygienic fields that come from different syntax contexts.

Fixes rust-lang#116334.
@bors bors closed this as completed in a9a389c Oct 5, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 5, 2023
Rollup merge of rust-lang#116429 - fmease:clean-up-struct-field-suggs, r=compiler-errors

Diagnostics: Be more careful when suggesting struct fields

Consolidate the various places which filter out struct fields that shouldn't be suggested into a single function.

Previously, each of those code paths had slightly different and incomplete metrics for no good reason. Now, there's only a single 'complete' metric (namely `is_field_suggestable`) which also filters out hygienic fields that come from different syntax contexts.

Fixes rust-lang#116334.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros-2.0 Area: Declarative macros 2.0 (#39412) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-decl_macro `#![feature(decl_macro)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants