Rust: Erroneous self arguments on bare functions emit subpar compilation error

Created on 15 Nov 2018  路  5Comments  路  Source: rust-lang/rust

When refactoring methods as bare functions and forgetting to remove the self argument, the compiler fails to parse the new function entirely instead of emitting a better error.

error: expected one of `::` or `:`, found `,`
    --> components/canvas_traits/webgl.rs:1000:10
     |
1000 |     &self,
     |          ^ expected one of `::` or `:` here
A-diagnostics A-parser E-mentor

Most helpful comment

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L5386

should have a call on the first argument to

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L5463

which if the result is Ok(Some(_)) a custom diagnostic should be raised pointing at the argument.

You can look at

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L5567

to see how trait fns parse arguments.

You can see how a diagnostic is created here

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L803-L821

and after that all that is needed for that error to be outputted is to call err.emit().


https://forge.rust-lang.org has information on the compiler itself, you'll want to at the very least look at https://forge.rust-lang.org/x-py.html.

All 5 comments

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L5386

should have a call on the first argument to

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L5463

which if the result is Ok(Some(_)) a custom diagnostic should be raised pointing at the argument.

You can look at

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L5567

to see how trait fns parse arguments.

You can see how a diagnostic is created here

https://github.com/rust-lang/rust/blob/9649c1f70fddd01843024932df97fb5a2b10bfe8/src/libsyntax/parse/parser.rs#L803-L821

and after that all that is needed for that error to be outputted is to call err.emit().


https://forge.rust-lang.org has information on the compiler itself, you'll want to at the very least look at https://forge.rust-lang.org/x-py.html.

Hello, I would like to work on this

As this is also causing the same error message, I think that it might actually be better to check for self in every argument position:

struct Foo {}

impl Foo {
    fn foo(a: u32, &mut self) {

    }
}

fn bar(a: u32, self) {}

playground

@Axary

As this is also causing the same error message

That is a good point. It's probably a good idea to have _different_ error message for the (so far) three cases. On Foo::foo, you should complain that self should appear first, if there are many that there should be one, in bar you should complain that self is only valid in trait fns.

Ok :+1: will do

Was this page helpful?
0 / 5 - 0 ratings