Skip to content

Type inference issue when destructuring a struct #8783

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

Closed
sfackler opened this issue Aug 27, 2013 · 4 comments · Fixed by #18171
Closed

Type inference issue when destructuring a struct #8783

sfackler opened this issue Aug 27, 2013 · 4 comments · Fixed by #18171

Comments

@sfackler
Copy link
Member

The program

extern mod extra;

use extra::url::Url;

fn main() {
    let Url { host, port, _ } = FromStr::from_str("localhost:1234").unwrap();
}

fails to compile with the following error message:

test.rs:6:5: 6:26 error: the type of this value must be known in this context
test.rs:6   let Url { host, port, _ } = FromStr::from_str("localhost:1234").unwrap();
                ^~~~~~~~~~~~~~~~~~~~~
test.rs:6:29: 6:46 error: failed to find an implementation of trait std::from_str::FromStr for [type error]
test.rs:6   let Url { host, port, _ } = FromStr::from_str("localhost:1234").unwrap();
                                        ^~~~~~~~~~~~~~~~~

even though the compiler should be able to infer that it's looking for the FromStr implementation for Url.

@metajack
Copy link
Contributor

nominating feature complete

@nikomatsakis
Copy link
Contributor

In general we require known types upon entering pattern matching, but you are correct it is stricter than necessary.

@catamorphism
Copy link
Contributor

Just a bug

@Aatch
Copy link
Contributor

Aatch commented Mar 4, 2014

Visiting for triage.

Wouldn't say it's a bug, per se, more a limitation in the current system.

bors added a commit that referenced this issue Oct 25, 2014
Rather than doing it top-down, with a known expected type, we will now simply establish the appropriate constraints between the pattern and the expression it destructures.

Closes #8783.
Closes #10200.
flip1995 pushed a commit to flip1995/rust that referenced this issue May 5, 2022
Move only_used_in_recursion to nursery

r? `@llogiq`

I still think this is the right thing to do for this lint. See: rust-lang#8782

I want to get this merged before the sync on Thursday, because I want to backport this and the last fix rust-lang#8691 to beta.

changelog: Move [`only_used_in_recursion`] to nursery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants