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

Misleading error on tuple struct field with docstring #122463

Open
tbillington opened this issue Mar 14, 2024 · 3 comments
Open

Misleading error on tuple struct field with docstring #122463

tbillington opened this issue Mar 14, 2024 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue

Comments

@tbillington
Copy link

I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=48a7a316c9d4825be960974fd87a8d8d

struct Foo {
    a: Vec<(
        /// Docstring
        f32,
        f32,
    )>,
}

I expected to see this happen: Add docstring to field in tuple struct

Instead, this happened: Misleading second error "constant provided when a type was expected".

   Compiling playground v0.0.1 (/playground)
error: expected type, found doc comment `/// Docstring`
 --> src/lib.rs:3:9
  |
3 |         /// Docstring
  |         ^^^^^^^^^^^^^ expected type
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
2 ~     a: Vec<{ (
3 |         /// Docstring
4 |         f32,
5 |         f32,
6 ~     ) }>,
  |

error[E0747]: constant provided when a type was expected
 --> src/lib.rs:2:12
  |
2 |       a: Vec<(
  |  ____________^
3 | |         /// Docstring
4 | |         f32,
5 | |         f32,
6 | |     )>,
  | |_____^

Meta

rustc --version --verbose:

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: aarch64-apple-darwin
release: 1.76.0
LLVM version: 17.0.6
@tbillington tbillington added the C-bug Category: This is a bug. label Mar 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 14, 2024
@tbillington
Copy link
Author

As an aside, would docstrings in "nested" tuples like this be a feature request? Docstrings are supported in tuple structs as of #87451.

@jieyouxu jieyouxu added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-confusing Diagnostics: Confusing error or lint that should be reworked. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 14, 2024
@workingjubilee
Copy link
Member

@tbillington This isn't a docstring on a tuple, this is a docstring on a generic. There is no "actual" tuple type there because it's effectively an argument to the Vec. If you want to document this type, you should probably separately declare and document its fields.

@workingjubilee
Copy link
Member

This code works:

struct Foo {
    a: Vec<(
        // Comment
        f32,
        f32,
    )>,
}

So I suspect the error has something to do with the fact that a type in this position won't successfully have an attribute attached to it, because /// desugars into

#[doc = ""]

I believe the diagnostics should be updated to inform people of this.

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-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue
Projects
None yet
Development

No branches or pull requests

4 participants