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

Diagnostic when "constructing" something with ::new instead of ::new() should be better. #31341

Closed
vi opened this issue Feb 1, 2016 · 1 comment · Fixed by #33325
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@vi
Copy link
Contributor

vi commented Feb 1, 2016

use std::collections::BTreeMap;

fn main() {
    let mut q = BTreeMap::new;
    for i in 0..1000_000_000 {
        q.insert(i, [0u8; 65536]);
    }
}
$ rustc heapoverflow.rs 
heapoverflow.rs:6:11: 6:34 error: no method named `insert` found for type `fn() -> collections::btree::map::BTreeMap<_, _> {collections::btree::map::BTreeMap<K, V>::new}` in the current scope
heapoverflow.rs:6         q.insert(i, [0u8; 65536]);
                            ^~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

At first I tried add instead of insert, then looked at docs, changed to insert again, then tried deliberately invalid inse3rt to ensure that error message does not change. Then I noticed fn() in error message and after little thinking found the issue.

Maybe such simple mistake should have friendlier error message? For example, if obj.method does not exist, but obj().method does, it can be mentioned somehow.

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 2, 2016
@jonas-schievink
Copy link
Contributor

This is now somewhat fixed:

<anon>:6:11: 6:17 error: no method named `insert` found for type `fn() -> std::collections::BTreeMap<_, _> {<std::collections::BTreeMap<K, V>><_, _>::new}` in the current scope
<anon>:6         q.insert(i, [0u8; 65536]);
                   ^~~~~~
<anon>:6:9: 6:10 note: q is a function, perhaps you wish to call it
<anon>:6:9: 6:10 help: try calling the base function:
<anon>:          q().insert(i, [0u8; 65536]);
error: aborting due to previous error

I found "try calling the base function" to be confusing. What's a "base" function?

Another problem is that this suggests q() even when q takes arguments:

fn myfn(a: u8) { loop {} }

fn main() {
    let mut q = myfn;
    for i in 0..1000_000_000 {
        q.insert(i, [0u8; 65536]);
    }
}
<anon>:6:11: 6:17 error: no method named `insert` found for type `fn(u8) {myfn}` in the current scope
<anon>:6         q.insert(i, [0u8; 65536]);
                   ^~~~~~
<anon>:6:9: 6:10 note: q is a function, perhaps you wish to call it
<anon>:6:9: 6:10 help: try calling the base function:
<anon>:          q().insert(i, [0u8; 65536]);
error: aborting due to previous error

birkenfeld added a commit to birkenfeld/rust that referenced this issue May 1, 2016
* It is not clear what a "base function" is.
* The suggestion just adds parens, so suggests calling without args.

The second point could be fixed with e.g. `(...)` instead of `()`,
but the preceding "note: X is a function, perhaps you wish to call it"
should already be clear enough.

Fixes: rust-lang#31341
Manishearth added a commit to Manishearth/rust that referenced this issue May 2, 2016
typeck: remove confusing suggestion for calling a fn type

* It is not clear what a "base function" is.
* The suggestion just adds parens, so suggests calling without args.

The second point could be fixed with e.g. `(...)` instead of `()`,
but the preceding "note: X is a function, perhaps you wish to call it"
should already be clear enough.

Fixes: rust-lang#31341
Manishearth added a commit to Manishearth/rust that referenced this issue May 3, 2016
typeck: remove confusing suggestion for calling a fn type

* It is not clear what a "base function" is.
* The suggestion just adds parens, so suggests calling without args.

The second point could be fixed with e.g. `(...)` instead of `()`,
but the preceding "note: X is a function, perhaps you wish to call it"
should already be clear enough.

Fixes: rust-lang#31341
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants