Skip to content

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

Closed
@vi

Description

@vi
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions