Skip to content

[WIP] Remove type ascription syntax #106826

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

Closed
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,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 @@ -611,7 +611,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
21 changes: 8 additions & 13 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`
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 @@ -312,7 +308,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 @@ -336,10 +336,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
1 change: 0 additions & 1 deletion compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ pub fn parse_asm_args<'a>(
// After a template string, we always expect *only* a comma...
let mut err = diag.struct_span_err(p.token.span, "expected token: `,`");
err.span_label(p.token.span, "expected `,`");
p.maybe_annotate_with_ascription(&mut err, false);
return Err(err);
} else {
// ...after that delegate to `expect` to also include the other expected tokens.
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 @@ -59,7 +59,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
8 changes: 8 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parse.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,14 @@ 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 = 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
2 changes: 0 additions & 2 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,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 @@ -1040,7 +1039,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 @@ -239,8 +239,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 @@ -1413,10 +1413,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
22 changes: 22 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,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