The editions only affect the syntax and do not change how the compiler will compile the code. The parser is pretty much the only edition-aware component of the compiler, with a few checks for the edition to decide how to parse stuffs. The AST is identical for both editions, although spans are aware of their editions and a few things in the compiler check which edition they were using. HIR and MIR do not need to know about editions.
Eg. for new keywords:
/// Returns `true` if the token is a keyword used in the language.
pub fn is_used_keyword(self) -> bool {
// Note: `span.edition()` is relatively expensive, don't call it unless necessary.
self.name >= kw::As && self.name <= kw::While ||
self.name.is_used_keyword_2018() && self.span.rust_2018()
}
The extra self.name.is_used_keyword_2018() && self.span.rust_2018()
will check whether the keyword is a keyword added in the 2018 edition (eg. dyn
), and for 2015, it will be considered as a regular symbol.
Another example is that in 2015, it was possible to omit parameter names in trait declaration, which is now forbidden. This is handled transparently as well:
// We don't allow argument names to be left off in edition 2018.
let is_name_required = p.token.span.rust_2018();
p.parse_arg_general(true, false, |_| is_name_required)
Then another piece of code will emit an error in this case, but only for 2018:
if require_name && (
is_trait_item ||
self.token == token::Comma ||
self.token == token::CloseDelim(token::Paren)
) { // `fn foo(a, b) {}` or `fn foo(usize, usize) {}`
err.span_suggestion(
pat.span,
"if this was a parameter name, give it a type",
format!("{}: TypeName", ident),
Applicability::HasPlaceholders,
);
err.span_suggestion(
pat.span,
"if this is a type, explicitly ignore the parameter name",
format!("_: {}", ident),
Applicability::MachineApplicable,
);
err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
return Some(ident);
}
and for 2015, a dummy name is created instead:
let ident = Ident::new(kw::Invalid, self.prev_span);
let pat = P(Pat {
id: ast::DUMMY_NODE_ID,
node: PatKind::Ident(
BindingMode::ByValue(Mutability::Immutable), ident, None),
span: ty.span,
});
The rest of the compiler doesn't need to know whether the user actually provided a name or not.