Skip to content

Importing libraries leads to fully qualified types being shown unnecessarily #113933

Open
@Wilfred

Description

@Wilfred

Code

use clap::Parser;

fn wrong_type() -> Option<String> {
    return true;
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:4:12
  |
3 | fn wrong_type() -> Option<String> {
  |                    -------------- expected `Option<std::string::String>` because of return type
4 |     return true;
  |            ^^^^ expected `Option<String>`, found `bool`
  |
  = note: expected enum `Option<std::string::String>`
             found type `bool`

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:4:12
  |
3 | fn wrong_type() -> Option<String> {
  |                    -------------- expected `Option<String>` because of return type
4 |     return true;
  |            ^^^^ expected `Option<String>`, found `bool`
  |
  = note: expected enum `Option<String>`
             found type `bool`

Rationale and extra context

Adding imports, even though they don't introduce another String type in the current file, is making rustc print fully qualified type names.

Clap doesn't define any types called String, although it does have a variant named String elsewhere in the library: https://docs.rs/clap/latest/clap/error/enum.ContextValue.html#variant.String

Other cases

There's a similar issue with serde and option:

use serde::Deserialize;

fn wrong_type() -> Option<String> {
    return true;
}

rustc output:

error[E0308]: mismatched types
 --> src/lib.rs:4:12
  |
3 | fn wrong_type() -> Option<String> {
  |                    -------------- expected `std::option::Option<std::string::String>` because of return type
4 |     return true;
  |            ^^^^ expected `Option<String>`, found `bool`
  |
  = note: expected enum `std::option::Option<std::string::String>`
             found type `bool`

Anything else?

Playground links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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