4

I understand the goal of Editions and how to use them, but the documentation does not shed any light on their inner workings.

Assume that I have two source files:

  • old.rs, targeting Rust 2015;

  • new.rs, targeting Rust 2018.

I want to build an application that consumes both of them. What mechanism is being used to ensure they can interoperate?

Do both old.rs and new.rs get translated to the same HIR model? Or does the homogenisation happen before (e.g. AST level) or after (e.g. MIR level) that?

1
  • The linked page says Edition changes only affect the way the compiler initially parses the code. This suggests the difference probably goes no farther than the AST.
    – trent
    Aug 2, 2019 at 20:39

1 Answer 1

5

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.