Skip to content

Commit

Permalink
Auto merge of rust-lang#109128 - chenyukang:yukang/remove-type-ascrip…
Browse files Browse the repository at this point in the history
…tion, r=estebank

Remove type ascription from parser and diagnostics

Mostly based on rust-lang#106826

Part of rust-lang#101728

r? `@estebank`
  • Loading branch information
bors committed May 2, 2023
2 parents 7b99493 + 5d1796a commit 98c33e4
Show file tree
Hide file tree
Showing 97 changed files with 752 additions and 1,140 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,6 @@ pub enum ClosureBinder {
pub struct MacCall {
pub path: Path,
pub args: P<DelimArgs>,
pub prior_type_ascription: Option<(Span, bool)>,
}

impl MacCall {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
}

pub fn noop_visit_mac<T: MutVisitor>(mac: &mut MacCall, vis: &mut T) {
let MacCall { path, args, prior_type_ascription: _ } = mac;
let MacCall { path, args } = mac;
vis.visit_path(path);
visit_delim_args(args, vis);
}
Expand Down
23 changes: 9 additions & 14 deletions compiler/rustc_ast/src/util/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ pub enum AssocOp {
DotDot,
/// `..=` range
DotDotEq,
/// `:`
Colon,
}

#[derive(PartialEq, Debug)]
Expand Down Expand Up @@ -96,7 +94,6 @@ impl AssocOp {
token::DotDotEq => Some(DotDotEq),
// DotDotDot is no longer supported, but we need some way to display the error
token::DotDotDot => Some(DotDotEq),
token::Colon => Some(Colon),
// `<-` should probably be `< -`
token::LArrow => Some(Less),
_ if t.is_keyword(kw::As) => Some(As),
Expand Down Expand Up @@ -133,7 +130,7 @@ impl AssocOp {
pub fn precedence(&self) -> usize {
use AssocOp::*;
match *self {
As | Colon => 14,
As => 14,
Multiply | Divide | Modulus => 13,
Add | Subtract => 12,
ShiftLeft | ShiftRight => 11,
Expand All @@ -156,7 +153,7 @@ impl AssocOp {
Assign | AssignOp(_) => Fixity::Right,
As | Multiply | Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd
| BitXor | BitOr | Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual
| LAnd | LOr | Colon => Fixity::Left,
| LAnd | LOr => Fixity::Left,
DotDot | DotDotEq => Fixity::None,
}
}
Expand All @@ -166,8 +163,9 @@ impl AssocOp {
match *self {
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => true,
Assign | AssignOp(_) | As | Multiply | Divide | Modulus | Add | Subtract
| ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot | DotDotEq
| Colon => false,
| ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot | DotDotEq => {
false
}
}
}

Expand All @@ -177,7 +175,7 @@ impl AssocOp {
Assign | AssignOp(_) => true,
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | As | Multiply
| Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd | BitXor
| BitOr | LAnd | LOr | DotDot | DotDotEq | Colon => false,
| BitOr | LAnd | LOr | DotDot | DotDotEq => false,
}
}

Expand All @@ -202,7 +200,7 @@ impl AssocOp {
BitOr => Some(BinOpKind::BitOr),
LAnd => Some(BinOpKind::And),
LOr => Some(BinOpKind::Or),
Assign | AssignOp(_) | As | DotDot | DotDotEq | Colon => None,
Assign | AssignOp(_) | As | DotDot | DotDotEq => None,
}
}

Expand All @@ -223,10 +221,9 @@ impl AssocOp {
Greater | // `{ 42 } > 3`
GreaterEqual | // `{ 42 } >= 3`
AssignOp(_) | // `{ 42 } +=`
As | // `{ 42 } as usize`
// Equal | // `{ 42 } == { 42 }` Accepting these here would regress incorrect
// NotEqual | // `{ 42 } != { 42 }` struct literals parser recovery.
Colon, // `{ 42 }: usize`
// NotEqual | // `{ 42 } != { 42 } struct literals parser recovery.
As // `{ 42 } as usize`
)
}
}
Expand Down Expand Up @@ -254,7 +251,6 @@ pub enum ExprPrecedence {
Binary(BinOpKind),

Cast,
Type,

Assign,
AssignOp,
Expand Down Expand Up @@ -313,7 +309,6 @@ impl ExprPrecedence {
// Binop-like expr kinds, handled by `AssocOp`.
ExprPrecedence::Binary(op) => AssocOp::from_ast_binop(op).precedence() as i8,
ExprPrecedence::Cast => AssocOp::As.precedence() as i8,
ExprPrecedence::Type => AssocOp::Colon.precedence() as i8,

ExprPrecedence::Assign |
ExprPrecedence::AssignOp => AssocOp::Assign.precedence() as i8,
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,16 @@ impl<'a> State<'a> {
self.print_type(ty);
}
ast::ExprKind::Type(expr, ty) => {
let prec = AssocOp::Colon.precedence() as i8;
self.print_expr_maybe_paren(expr, prec);
self.word_space(":");
self.word("type_ascribe!(");
self.ibox(0);
self.print_expr(expr);

self.word(",");
self.space_if_not_bol();
self.print_type(ty);

self.end();
self.word(")");
}
ast::ExprKind::Let(pat, scrutinee, _) => {
self.print_let(pat, scrutinee);
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ pub fn parse_asm_args<'a>(
if !p.eat(&token::Comma) {
if allow_templates {
// After a template string, we always expect *only* a comma...
let mut err = diag.create_err(errors::AsmExpectedComma { span: p.token.span });
p.maybe_annotate_with_ascription(&mut err, false);
return Err(err);
return Err(diag.create_err(errors::AsmExpectedComma { span: p.token.span }));
} else {
// ...after that delegate to `expect` to also include the other expected tokens.
return Err(p.expect(&token::Comma).err().unwrap());
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub fn expand_assert<'cx>(
delim: MacDelimiter::Parenthesis,
tokens,
}),
prior_type_ascription: None,
})),
);
expr_if_not(cx, call_site_span, cond_expr, then, None)
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ impl<'cx, 'a> Context<'cx, 'a> {
delim: MacDelimiter::Parenthesis,
tokens: initial.into_iter().chain(captures).collect::<TokenStream>(),
}),
prior_type_ascription: None,
})),
)
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_builtin_macros/src/edition_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ fn expand<'cx>(
delim: MacDelimiter::Parenthesis,
tokens: tts,
}),
prior_type_ascription: None,
})),
),
)
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,6 @@ pub struct ExpansionData {
pub depth: usize,
pub module: Rc<ModuleData>,
pub dir_ownership: DirOwnership,
pub prior_type_ascription: Option<(Span, bool)>,
/// Some parent node that is close to this macro call
pub lint_node_id: NodeId,
pub is_trailing_mac: bool,
Expand Down Expand Up @@ -1043,7 +1042,6 @@ impl<'a> ExtCtxt<'a> {
depth: 0,
module: Default::default(),
dir_ownership: DirOwnership::Owned { relative: None },
prior_type_ascription: None,
lint_node_id: ast::CRATE_NODE_ID,
is_trailing_mac: false,
},
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
}
SyntaxExtensionKind::LegacyBang(expander) => {
let prev = self.cx.current_expansion.prior_type_ascription;
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
let tok_result = expander.expand(self.cx, span, mac.args.tokens.clone());
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
result
} else {
self.error_wrong_fragment_kind(fragment_kind, &mac, span);
fragment_kind.dummy(span)
};
self.cx.current_expansion.prior_type_ascription = prev;
result
}
_ => unreachable!(),
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ fn expand_macro<'cx>(
trace_macros_note(&mut cx.expansions, sp, msg);
}

let mut p = Parser::new(sess, tts, false, None);
p.last_type_ascription = cx.current_expansion.prior_type_ascription;
let p = Parser::new(sess, tts, false, None);

if is_local {
cx.resolver.record_macro_rule_usage(node_id, i);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_expand/src/placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn placeholder(
delim: ast::MacDelimiter::Parenthesis,
tokens: ast::tokenstream::TokenStream::new(Vec::new()),
}),
prior_type_ascription: None,
})
}

Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,10 +1407,16 @@ impl<'a> State<'a> {
self.print_type(ty);
}
hir::ExprKind::Type(expr, ty) => {
let prec = AssocOp::Colon.precedence() as i8;
self.print_expr_maybe_paren(expr, prec);
self.word_space(":");
self.word("type_ascribe!(");
self.ibox(0);
self.print_expr(expr);

self.word(",");
self.space_if_not_bol();
self.print_type(ty);

self.end();
self.word(")");
}
hir::ExprKind::DropTemps(init) => {
// Print `{`:
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,9 @@ enum ArmType {
/// For example, if we are constructing a witness for the match against
///
/// ```compile_fail,E0004
/// # #![feature(type_ascription)]
/// struct Pair(Option<(u32, u32)>, bool);
/// # fn foo(p: Pair) {
/// match (p: Pair) {
/// match p {
/// Pair(None, _) => {}
/// Pair(_, false) => {}
/// }
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ parse_maybe_fn_typo_with_impl = you might have meant to write `impl` instead of
parse_expected_fn_path_found_fn_keyword = expected identifier, found keyword `fn`
.suggestion = use `Fn` to refer to the trait
parse_path_single_colon = path separator must be a double colon
.suggestion = use a double colon instead
parse_colon_as_semi = statements are terminated with a semicolon
.suggestion = use a semicolon instead
parse_type_ascription_removed =
if you meant to annotate an expression with a type, the type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
parse_where_clause_before_tuple_struct_body = where clauses are not allowed before tuple struct bodies
.label = unexpected where clause
.name_label = while parsing this tuple struct
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,28 @@ pub(crate) struct ExpectedFnPathFoundFnKeyword {
pub fn_token_span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_path_single_colon)]
pub(crate) struct PathSingleColon {
#[primary_span]
#[suggestion(applicability = "machine-applicable", code = "::")]
pub span: Span,

#[note(parse_type_ascription_removed)]
pub type_ascription: Option<()>,
}

#[derive(Diagnostic)]
#[diag(parse_colon_as_semi)]
pub(crate) struct ColonAsSemi {
#[primary_span]
#[suggestion(applicability = "machine-applicable", code = ";")]
pub span: Span,

#[note(parse_type_ascription_removed)]
pub type_ascription: Option<()>,
}

#[derive(Diagnostic)]
#[diag(parse_where_clause_before_tuple_struct_body)]
pub(crate) struct WhereClauseBeforeTupleStructBody {
Expand Down
Loading

0 comments on commit 98c33e4

Please sign in to comment.