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

Diagnostic suggests adding : ?Sized in an incorrect place if a type parameter default is present #120878

Closed
OdenShirataki opened this issue Feb 10, 2024 · 3 comments · Fixed by #120915
Assignees
Labels
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@OdenShirataki
Copy link
Contributor

OdenShirataki commented Feb 10, 2024

Code

fn main() {
    struct StructA<A, B = A> {
        _marker: std::marker::PhantomData<fn() -> (A, B)>,
    }

    struct StructB {
        a: StructA<isize, [u8]>,
    }
}

Current output

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src\main.rs:7:12
  |
7 |         a: StructA<isize, [u8]>,
  |            ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `StructA`
 --> src\main.rs:2:23
  |
2 |     struct StructA<A, B = A> {
  |                       ^^^^^ required by this bound in `StructA`
help: consider relaxing the implicit `Sized` restriction
  |
2 |     struct StructA<A, B = A: ?Sized> {
  |                            ++++++++

Desired output

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src\main.rs:7:12
  |
7 |         a: StructA<isize, [u8]>,
  |            ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `StructA`
 --> src\main.rs:2:23
  |
2 |     struct StructA<A, B = A> {
  |                       ^^^^^ required by this bound in `StructA`
help: consider relaxing the implicit `Sized` restriction
  |
2 |     struct StructA<A, B: ?Sized = A> {
  |                            ++++++++

Rationale and extra context

No response

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-pc-windows-msvc
release: 1.76.0
LLVM version: 17.0.6

Anything else?

No response

@OdenShirataki OdenShirataki added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 10, 2024
@kyleguarco
Copy link

kyleguarco commented Feb 10, 2024

Another variant of this error I came up with using stable 1.76.0

struct S<A, B = A>(std::marker::PhantomData<(A, B)>);
struct T<A, B: ?Sized>(S<A, B>);
error[E0277]: the size for values of type `B` cannot be known at compilation time
 --> test.rs:2:24
  |
2 | struct T<A, B: ?Sized>(S<A, B>);
  |             -          ^^^^^^^ doesn't have a size known at compile-time
  |             |
  |             this type parameter needs to be `Sized`
  |
note: required by a bound in `S`
 --> test.rs:1:13
  |
1 | struct S<A, B = A>(std::marker::PhantomData<(A, B)>);
  |             ^^^^^ required by this bound in `S`
help: consider removing the `?Sized` bound to make the type parameter `Sized`
  |
2 - struct T<A, B: ?Sized>(S<A, B>);
2 + struct T<A, B>(S<A, B>);
  |
help: consider relaxing the implicit `Sized` restriction
  |
1 | struct S<A, B = A: ?Sized>(std::marker::PhantomData<(A, B)>);
  |                  ++++++++

Edit Just tested on stable 1.75.0 and 1.74.0. Both produced the same output.

@OdenShirataki
Copy link
Contributor Author

@rustbot claim

@fmease fmease changed the title the size for values of type [u8] cannot be known at compilation time (with default type parameter in struct) Diagnostic suggests adding : ?Sized in an incorrect place if a type parameter default is present Feb 10, 2024
@fmease fmease added 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 Feb 10, 2024
@fmease
Copy link
Member

fmease commented Feb 10, 2024

This is only tangentially related but just to give more context to anyone stumbling upon this issue: The diagnostic highlighting the entire path StructA<isize, [u8]> instead of only the type argument [u8] is tracked in #105306.

OdenShirataki added a commit to OdenShirataki/rust that referenced this issue Feb 11, 2024
change span.hi if exists default.

Diagnostic suggests adding : ?Sized in an incorrect place if a type parameter default is present
OdenShirataki added a commit to OdenShirataki/rust that referenced this issue Feb 11, 2024
…default exists.

change span.hi if exists default.

Diagnostic suggests adding : ?Sized in an incorrect place if a type parameter default is present
OdenShirataki added a commit to OdenShirataki/rust that referenced this issue Feb 11, 2024
fix it,
Diagnostic suggests adding : ?Sized in an incorrect place if a type parameter default is present

fix: rust-lang#120878 Use param.name.indent().span if type parameter default exists.

change span.hi if exists default.

Diagnostic suggests adding : ?Sized in an incorrect place if a type parameter default is present

If there are no bounds, you should always use param.name, so there is no need to branch.

add test

add test
@bors bors closed this as completed in f3e66ed Feb 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 14, 2024
Rollup merge of rust-lang#120915 - OdenShirataki:master, r=fmease

Fix suggestion span for `?Sized` when param type has default

Fixes rust-lang#120878

Diagnostic suggests adding `: ?Sized` in an incorrect place if a type parameter default is present

r? `@fmease`
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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