I Feel Personally Victimized by the Rust Lexer
17 Oct 2017
I just spent way too long trying to figure out a missing comma.
Say you have some code that looks like the following (note the missing comma between &self.srcaddr
and &self.dstaddr
)
fn output(&self) -> String { format!("{} -> {} ({})", &self.srcaddr &self.dstaddr, &self.nexthop) }
What error would you expect the compiler to spit out?
Something like expected token: ','
?
What about invalid reference to argument '3' (there are 3 arguments)
?
It turns out that since whitespace is ignored, the second ampersand in the above code is interpreted as a bitwise AND, with one reference and one variable as its operands. Rather than complain about a missing token, the compiler interprets what it sees as valid syntax, and indeed it ends up with one less argument than it expects based on the provided format string.