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

"try using a variant of the expected type" suggesting a peculiar syntax #65494

Closed
Patryk27 opened this issue Oct 17, 2019 · 3 comments · Fixed by #65562
Closed

"try using a variant of the expected type" suggesting a peculiar syntax #65494

Patryk27 opened this issue Oct 17, 2019 · 3 comments · Fixed by #65562
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented Oct 17, 2019

Hi,

I've got following code:

#[derive(Eq, PartialEq)]
pub struct Foo;

impl Foo {
    fn new() -> Self { Foo }
}

fn main() {
    if None == Foo::new() {
            // ^^^^^^^^^^
            // |
            // expected enum `std::option::Option`, found struct `Foo`
            // help: try using a variant of the expected type: `Some(<Foo>::new())`
    }
}

(playground link)

The suggested <Foo>::new() syntax is valid, but - quite frankly - spooky (and it seems to be this way from the very beginning of this feature). Could it suggest just Foo::new()?

I'd like to tackle this myself, but I'd need at least a hint where to look :-)

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 17, 2019
@csmoe
Copy link
Member

csmoe commented Oct 17, 2019

improvement needed inside print_qpath:

rust/src/librustc/hir/print.rs

Lines 1491 to 1536 in f54911c

pub fn print_qpath(&mut self,
qpath: &hir::QPath,
colons_before_params: bool)
{
match *qpath {
hir::QPath::Resolved(None, ref path) => {
self.print_path(path, colons_before_params)
}
hir::QPath::Resolved(Some(ref qself), ref path) => {
self.s.word("<");
self.print_type(qself);
self.s.space();
self.word_space("as");
for (i, segment) in path.segments[..path.segments.len() - 1].iter().enumerate() {
if i > 0 {
self.s.word("::")
}
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident);
self.print_generic_args(segment.generic_args(),
segment.infer_args,
colons_before_params);
}
}
self.s.word(">");
self.s.word("::");
let item_segment = path.segments.last().unwrap();
self.print_ident(item_segment.ident);
self.print_generic_args(item_segment.generic_args(),
item_segment.infer_args,
colons_before_params)
}
hir::QPath::TypeRelative(ref qself, ref item_segment) => {
self.s.word("<");
self.print_type(qself);
self.s.word(">");
self.s.word("::");
self.print_ident(item_segment.ident);
self.print_generic_args(item_segment.generic_args(),
item_segment.infer_args,
colons_before_params)
}
}
}

@Patryk27
Copy link
Contributor Author

Thanks! I'll try to fix it tomorrow :-)

@estebank estebank added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Oct 17, 2019
@estebank
Copy link
Contributor

@Patryk27 consider also modifying the following to use span_to_snippet and only use pprint if that method returns Err:

let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));

That way we ensure that we suggest the code as it is in the user's code whenever that is possible, regardless of formatting they might be using and the idiosyncrasies of pprint.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 28, 2019
Improve the "try using a variant of the expected type" hint.

Fix rust-lang#65494.

- Change type-printing output.
- Use `span_to_snippet` when possible.
- Change the message to `try using a variant of the expected enum`
Centril added a commit to Centril/rust that referenced this issue Oct 29, 2019
Improve the "try using a variant of the expected type" hint.

Fix rust-lang#65494.

- Change type-printing output.
- Use `span_to_snippet` when possible.
- Change the message to `try using a variant of the expected enum`
Centril added a commit to Centril/rust that referenced this issue Oct 29, 2019
Improve the "try using a variant of the expected type" hint.

Fix rust-lang#65494.

- Change type-printing output.
- Use `span_to_snippet` when possible.
- Change the message to `try using a variant of the expected enum`
@bors bors closed this as completed in 9c5b6b2 Oct 29, 2019
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` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

4 participants