Skip to content

Note where implicit Sized requirement comes from #27964

Closed
@kornelski

Description

@kornelski

I was not aware that Sized is a default requirement for type parameters, so I was very confused why this code doesn't compile:

trait OopsSized<O /* missing : ?Sized */> {
    fn foo(&self) -> Option<Box<O>>;
}

trait Unsized {}

struct Foo;

impl OopsSized<Unsized> for Foo {
    fn foo(&self) -> Option<Box<Unsized>> {
        None
    }
}

I couldn't understand why compiler insists on having Sized for the innermost type, if Box doesn't care and makes the boxed type sized.

The current explanation for E0277 doesn't cover this case specifically, and it doesn't mention the unusual ?Sized syntax. It'd help me if, for example, the hint:

note: required by OopsSized

made implied defaults explicitly spelled out:

note: required by OopsSized<O: Sized>

and/or hinted how to fix it:

note: required by OopsSized, because O by default is Sized. Try O: ?Sized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions