Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse default async fn & Pre-expansion-gate default #63749

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 11 additions & 19 deletions src/libsyntax/feature_gate.rs
Expand Up @@ -1980,19 +1980,13 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}

ast::ItemKind::Impl(_, polarity, defaultness, _, _, _, _) => {
ast::ItemKind::Impl(_, polarity, ..) => {
if polarity == ast::ImplPolarity::Negative {
gate_feature_post!(&self, optin_builtin_traits,
i.span,
"negative trait bounds are not yet fully implemented; \
use marker types for now");
}

if let ast::Defaultness::Default = defaultness {
gate_feature_post!(&self, specialization,
i.span,
"specialization is unstable");
}
}

ast::ItemKind::Trait(ast::IsAuto::Yes, ..) => {
Expand Down Expand Up @@ -2231,12 +2225,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

fn visit_impl_item(&mut self, ii: &'a ast::ImplItem) {
if ii.defaultness == ast::Defaultness::Default {
gate_feature_post!(&self, specialization,
ii.span,
"specialization is unstable");
}

match ii.node {
ast::ImplItemKind::Method(..) => {}
ast::ImplItemKind::OpaqueTy(..) => {
Expand Down Expand Up @@ -2438,16 +2426,20 @@ pub fn check_crate(krate: &ast::Crate,
};

macro_rules! gate_all {
($gate:ident, $msg:literal) => { gate_all!($gate, $gate, $msg); };
($spans:ident, $gate:ident, $msg:literal) => {
for span in &*sess.$spans.borrow() { gate_feature!(&ctx, $gate, *span, $msg); }
for span in &*sess.gated_spans.$spans.borrow() {
gate_feature!(&ctx, $gate, *span, $msg);
}
}
}

gate_all!(param_attr_spans, param_attrs, "attributes on function parameters are unstable");
gate_all!(let_chains_spans, let_chains, "`let` expressions in this position are experimental");
gate_all!(async_closure_spans, async_closure, "async closures are unstable");
gate_all!(yield_spans, generators, "yield syntax is experimental");
gate_all!(or_pattern_spans, or_patterns, "or-patterns syntax is experimental");
gate_all!(param_attrs, "attributes on function parameters are unstable");
gate_all!(let_chains, "`let` expressions in this position are experimental");
gate_all!(async_closure, "async closures are unstable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gate_all!(async_closure, "async closures are unstable");
gate_all!(async_closures, "async closures are unstable");

Nit: everything else is plural or uncountable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right, but fixing it is not worth the churn of people switching to #![feature(async_closures)]?

gate_all!(yields, generators, "yield syntax is experimental");
gate_all!(or_patterns, "or-patterns syntax is experimental");
gate_all!(specialization, "specialization is unstable");

let visitor = &mut PostExpansionVisitor {
context: &ctx,
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/parse/attr.rs
Expand Up @@ -21,9 +21,8 @@ const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \
impl<'a> Parser<'a> {
crate fn parse_arg_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
let attrs = self.parse_outer_attributes()?;
attrs.iter().for_each(|a|
self.sess.param_attr_spans.borrow_mut().push(a.span)
);
self.sess.gated_spans.param_attrs.borrow_mut()
.extend(attrs.iter().map(|a| a.span));
Ok(attrs)
}

Expand Down
34 changes: 20 additions & 14 deletions src/libsyntax/parse/mod.rs
Expand Up @@ -39,6 +39,24 @@ crate mod unescape_error_reporting;

pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;

/// Collected spans during parsing for places where a certain feature was
/// used and should be feature gated accordingly in `check_crate`.
#[derive(Default)]
pub struct GatedSpans {
/// Spans collected for gating `param_attrs`, e.g. `fn foo(#[attr] x: u8) {}`.
pub param_attrs: Lock<Vec<Span>>,
/// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
pub let_chains: Lock<Vec<Span>>,
/// Spans collected for gating `async_closure`, e.g. `async || ..`.
pub async_closure: Lock<Vec<Span>>,
/// Spans collected for gating `yield e?` expressions (`generators` gate).
pub yields: Lock<Vec<Span>>,
/// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
pub or_patterns: Lock<Vec<Span>>,
/// Spans collected for gating the `default` qualifier for `specialization`.
pub specialization: Lock<Vec<Span>>,
}

/// Info about a parsing session.
pub struct ParseSess {
pub span_diagnostic: Handler,
Expand All @@ -58,16 +76,8 @@ pub struct ParseSess {
/// operation token that followed it, but that the parser cannot identify without further
/// analysis.
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
pub param_attr_spans: Lock<Vec<Span>>,
// Places where `let` exprs were used and should be feature gated according to `let_chains`.
pub let_chains_spans: Lock<Vec<Span>>,
// Places where `async || ..` exprs were used and should be feature gated.
pub async_closure_spans: Lock<Vec<Span>>,
// Places where `yield e?` exprs were used and should be feature gated.
pub yield_spans: Lock<Vec<Span>>,
pub injected_crate_name: Once<Symbol>,
// Places where or-patterns e.g. `Some(Foo | Bar)` were used and should be feature gated.
pub or_pattern_spans: Lock<Vec<Span>>,
pub gated_spans: GatedSpans,
}

impl ParseSess {
Expand All @@ -93,12 +103,8 @@ impl ParseSess {
buffered_lints: Lock::new(vec![]),
edition: ExpnId::root().expn_data().edition,
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
param_attr_spans: Lock::new(Vec::new()),
let_chains_spans: Lock::new(Vec::new()),
async_closure_spans: Lock::new(Vec::new()),
yield_spans: Lock::new(Vec::new()),
injected_crate_name: Once::new(),
or_pattern_spans: Lock::new(Vec::new()),
gated_spans: GatedSpans::default(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/parse/parser/expr.rs
Expand Up @@ -999,7 +999,7 @@ impl<'a> Parser<'a> {
}

let span = lo.to(hi);
self.sess.yield_spans.borrow_mut().push(span);
self.sess.gated_spans.yields.borrow_mut().push(span);
} else if self.eat_keyword(kw::Let) {
return self.parse_let_expr(attrs);
} else if is_span_rust_2018 && self.eat_keyword(kw::Await) {
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl<'a> Parser<'a> {
};
if asyncness.is_async() {
// Feature gate `async ||` closures.
self.sess.async_closure_spans.borrow_mut().push(self.prev_span);
self.sess.gated_spans.async_closure.borrow_mut().push(self.prev_span);
}

let capture_clause = self.parse_capture_clause();
Expand Down Expand Up @@ -1234,7 +1234,7 @@ impl<'a> Parser<'a> {

if let ExprKind::Let(..) = cond.node {
// Remove the last feature gating of a `let` expression since it's stable.
let last = self.sess.let_chains_spans.borrow_mut().pop();
let last = self.sess.gated_spans.let_chains.borrow_mut().pop();
debug_assert_eq!(cond.span, last.unwrap());
}

Expand All @@ -1252,7 +1252,7 @@ impl<'a> Parser<'a> {
|this| this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
)?;
let span = lo.to(expr.span);
self.sess.let_chains_spans.borrow_mut().push(span);
self.sess.gated_spans.let_chains.borrow_mut().push(span);
Ok(self.mk_expr(span, ExprKind::Let(pats, expr), attrs))
}

Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/parse/parser/item.rs
Expand Up @@ -825,6 +825,7 @@ impl<'a> Parser<'a> {
self.is_keyword_ahead(1, &[
kw::Impl,
kw::Const,
kw::Async,
kw::Fn,
kw::Unsafe,
kw::Extern,
Expand All @@ -833,6 +834,7 @@ impl<'a> Parser<'a> {
])
{
self.bump(); // `default`
self.sess.gated_spans.specialization.borrow_mut().push(self.prev_span);
Defaultness::Default
} else {
Defaultness::Final
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser/pat.rs
Expand Up @@ -123,7 +123,7 @@ impl<'a> Parser<'a> {

let or_pattern_span = lo.to(self.prev_span);

self.sess.or_pattern_spans.borrow_mut().push(or_pattern_span);
self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);

Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
}
Expand Down
@@ -1,10 +1,8 @@
error[E0658]: specialization is unstable
--> $DIR/specialization-feature-gate-default.rs:7:1
|
LL | / default impl<T> Foo for T {
LL | | fn foo(&self) {}
LL | | }
| |_^
LL | default impl<T> Foo for T {
| ^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/31844
= help: add `#![feature(specialization)]` to the crate attributes to enable
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/specialization/issue-63716-parse-async.rs
@@ -0,0 +1,14 @@
// Ensure that `default async fn` will parse.
// See issue #63716 for details.

// check-pass
// edition:2018

#![feature(specialization)]

fn main() {}

#[cfg(FALSE)]
impl Foo for Bar {
default async fn baz() {}
}
Expand Up @@ -6,8 +6,16 @@ trait Foo {
fn foo(&self);
}

#[cfg(FALSE)]
impl<T> Foo for T {
default fn foo(&self) {} //~ ERROR specialization is unstable
default //~ ERROR specialization is unstable
fn foo(&self) {}
}

#[cfg(FALSE)]
default //~ ERROR specialization is unstable
impl<T> Foo for T {
fn foo(&self) {}
}

fn main() {}
@@ -1,12 +1,21 @@
error[E0658]: specialization is unstable
--> $DIR/specialization-feature-gate-default.rs:10:5
--> $DIR/specialization-feature-gate-default.rs:11:5
|
LL | default fn foo(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
LL | default
| ^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/31844
= help: add `#![feature(specialization)]` to the crate attributes to enable

error: aborting due to previous error
error[E0658]: specialization is unstable
--> $DIR/specialization-feature-gate-default.rs:16:1
|
LL | default
| ^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/31844
= help: add `#![feature(specialization)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.