Skip to content

Commit

Permalink
pre-expansion gate type_ascription
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 23, 2019
1 parent e4ed886 commit 15a6c09
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
21 changes: 6 additions & 15 deletions src/libsyntax/feature_gate/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,21 +498,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}

fn visit_expr(&mut self, e: &'a ast::Expr) {
match e.kind {
ast::ExprKind::Type(..) => {
// To avoid noise about type ascription in common syntax errors, only emit if it
// is the *only* error.
if self.parse_sess.span_diagnostic.err_count() == 0 {
gate_feature_post!(&self, type_ascription, e.span,
"type ascription is experimental");
}
}
_ => {}
}
visit::walk_expr(self, e)
}

fn visit_pat(&mut self, pattern: &'a ast::Pat) {
match &pattern.kind {
PatKind::Slice(pats) => {
Expand Down Expand Up @@ -805,6 +790,12 @@ pub fn check_crate(krate: &ast::Crate,
gate_all!(label_break_value, "labels on blocks are unstable");
gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");

// To avoid noise about type ascription in common syntax errors,
// only emit if it is the *only* error. (Also check it last.)
if parse_sess.span_diagnostic.err_count() == 0 {
gate_all!(type_ascription, "type ascription is experimental");
}

visit::walk_crate(&mut visitor, krate);
}

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl<'a> Parser<'a> {
self.last_type_ascription = Some((self.prev_span, maybe_path));

lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?;
self.sess.gated_spans.type_ascription.borrow_mut().push(lhs.span);
continue
} else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
// If we didn’t have to handle `x..`/`x..=`, it would be pretty easy to
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/sess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ crate struct GatedSpans {
pub label_break_value: Lock<Vec<Span>>,
/// Spans collected for gating `box_syntax`, e.g. `box $expr`.
pub box_syntax: Lock<Vec<Span>>,
/// Spans collected for gating `type_ascription`, e.g. `42: usize`.
pub type_ascription: Lock<Vec<Span>>,
}

/// Info about a parsing session.
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/feature-gates/feature-gate-type_ascription.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Type ascription is unstable

fn main() {
#[cfg(FALSE)]
fn foo() {
let a = 10: u8; //~ ERROR type ascription is experimental
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0658]: type ascription is experimental
--> $DIR/feature-gate-type_ascription.rs:4:13
--> $DIR/feature-gate-type_ascription.rs:5:13
|
LL | let a = 10: u8;
| ^^^^^^
Expand Down

0 comments on commit 15a6c09

Please sign in to comment.