Skip to content

Misleading error when incorrectly initializing a struct using Monster() instead of Monster{...} #6702

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
fabiand opened this issue May 23, 2013 · 4 comments

Comments

@fabiand
Copy link
Contributor

fabiand commented May 23, 2013

struct Monster {
 damage: int
}

impl Monster {
 fn attack(&self) -> int {
  self.damage // Why do I have to use a return here?
 }
}

fn main()
{
 let m = Monster();
 m.attack();
}


$ rust run monster.rs                     
monster.rs:14:9: 14:16 error: unresolved name: `Monster`.           <---- This error does not help to solve the problem
monster.rs:14  let m = Monster();
                       ^~~~~~~
error: aborting due to previous error
@fabiand
Copy link
Contributor Author

fabiand commented May 23, 2013

I called Monster() instead of Monster{...} - the error which was raised did not help me to find the problem ... I'd have expected sth like "Intialize structs using Monster{...}" or so ... but b/c of that error I looked for hidden utf8 chars :)

@Thiez
Copy link
Contributor

Thiez commented May 23, 2013

'Monster' could also be a function name, so the compiler can't tell what it should do. Having said that, according to our style guide functions and methods should be lower case, so perhaps the compiler could give a more helpful message in this case. For example by assuming that you meant to initialize a struct when it encounters a function call that starts with a capital letter and is not a function, but does match the name of a struct.

@emberian
Copy link
Member

On incoming I get:

/home/cmr/entropy/res.rs:5:12: 5:22 error: expected function but found `Monster`
/home/cmr/entropy/res.rs:5     let m = Monster();
                                       ^~~~~~~~~~

@catamorphism
Copy link
Contributor

@Thiez We can actually do better, since in resolve, the compiler already knows what names correspond to structs when it's printing out an error message. I'm about to submit a pull request to improve the error message.

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.

4 participants