Skip to content

Commit a36b94d

Browse files
committed
Disallow cast with trailing braced macro in let-else
1 parent 75a34ca commit a36b94d

File tree

4 files changed

+124
-17
lines changed

4 files changed

+124
-17
lines changed

compiler/rustc_ast/src/util/classify.rs

+91-4
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,17 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
8181
}
8282
}
8383

84+
pub enum TrailingBrace<'a> {
85+
/// Trailing brace in a macro call, like the one in `x as *const brace! {}`.
86+
/// We will suggest changing the macro call to a different delimiter.
87+
MacCall(&'a ast::MacCall),
88+
/// Trailing brace in any other expression, such as `a + B {}`. We will
89+
/// suggest wrapping the innermost expression in parentheses: `a + (B {})`.
90+
Expr(&'a ast::Expr),
91+
}
92+
8493
/// If an expression ends with `}`, returns the innermost expression ending in the `}`
85-
pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
94+
pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
8695
loop {
8796
match &expr.kind {
8897
AddrOf(_, _, e)
@@ -111,10 +120,14 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
111120
| Struct(..)
112121
| TryBlock(..)
113122
| While(..)
114-
| ConstBlock(_) => break Some(expr),
123+
| ConstBlock(_) => break Some(TrailingBrace::Expr(expr)),
124+
125+
Cast(_, ty) => {
126+
break type_trailing_braced_mac_call(ty).map(TrailingBrace::MacCall);
127+
}
115128

116129
MacCall(mac) => {
117-
break (mac.args.delim == Delimiter::Brace).then_some(expr);
130+
break (mac.args.delim == Delimiter::Brace).then_some(TrailingBrace::MacCall(mac));
118131
}
119132

120133
InlineAsm(_) | OffsetOf(_, _) | IncludedBytes(_) | FormatArgs(_) => {
@@ -131,7 +144,6 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
131144
| MethodCall(_)
132145
| Tup(_)
133146
| Lit(_)
134-
| Cast(_, _)
135147
| Type(_, _)
136148
| Await(_, _)
137149
| Field(_, _)
@@ -148,3 +160,78 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
148160
}
149161
}
150162
}
163+
164+
/// If the type's last token is `}`, it must be due to a braced macro call, such
165+
/// as in `*const brace! { ... }`. Returns that trailing macro call.
166+
fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
167+
loop {
168+
match &ty.kind {
169+
ast::TyKind::MacCall(mac) => {
170+
break (mac.args.delim == Delimiter::Brace).then_some(mac);
171+
}
172+
173+
ast::TyKind::Ptr(mut_ty) | ast::TyKind::Ref(_, mut_ty) => {
174+
ty = &mut_ty.ty;
175+
}
176+
177+
ast::TyKind::BareFn(fn_ty) => match &fn_ty.decl.output {
178+
ast::FnRetTy::Default(_) => break None,
179+
ast::FnRetTy::Ty(ret) => ty = ret,
180+
},
181+
182+
ast::TyKind::Path(_, path) => match path_return_type(path) {
183+
Some(trailing_ty) => ty = trailing_ty,
184+
None => break None,
185+
},
186+
187+
ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds, _) => {
188+
match bounds.last() {
189+
Some(ast::GenericBound::Trait(bound, _)) => {
190+
match path_return_type(&bound.trait_ref.path) {
191+
Some(trailing_ty) => ty = trailing_ty,
192+
None => break None,
193+
}
194+
}
195+
Some(ast::GenericBound::Outlives(_)) | None => break None,
196+
}
197+
}
198+
199+
ast::TyKind::Slice(..)
200+
| ast::TyKind::Array(..)
201+
| ast::TyKind::Never
202+
| ast::TyKind::Tup(..)
203+
| ast::TyKind::Paren(..)
204+
| ast::TyKind::Typeof(..)
205+
| ast::TyKind::Infer
206+
| ast::TyKind::ImplicitSelf
207+
| ast::TyKind::CVarArgs
208+
| ast::TyKind::Pat(..)
209+
| ast::TyKind::Dummy
210+
| ast::TyKind::Err(..) => break None,
211+
212+
// These end in brace, but cannot occur in a let-else statement.
213+
// They are only parsed as fields of a data structure. For the
214+
// purpose of denying trailing braces in the expression of a
215+
// let-else, we can disregard these.
216+
ast::TyKind::AnonStruct(..) | ast::TyKind::AnonUnion(..) => break None,
217+
}
218+
}
219+
}
220+
221+
/// Returns the trailing return type in the given path, if it has one.
222+
///
223+
/// ```ignore (illustrative)
224+
/// ::std::ops::FnOnce(&str) -> fn() -> *const c_void
225+
/// ^^^^^^^^^^^^^^^^^^^^^
226+
/// ```
227+
fn path_return_type(path: &ast::Path) -> Option<&ast::Ty> {
228+
let last_segment = path.segments.last()?;
229+
let args = last_segment.args.as_ref()?;
230+
match &**args {
231+
ast::GenericArgs::Parenthesized(args) => match &args.output {
232+
ast::FnRetTy::Default(_) => None,
233+
ast::FnRetTy::Ty(ret) => Some(ret),
234+
},
235+
ast::GenericArgs::AngleBracketed(_) => None,
236+
}
237+
}

compiler/rustc_parse/src/parser/stmt.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ast::Label;
1515
use rustc_ast as ast;
1616
use rustc_ast::ptr::P;
1717
use rustc_ast::token::{self, Delimiter, TokenKind};
18-
use rustc_ast::util::classify;
18+
use rustc_ast::util::classify::{self, TrailingBrace};
1919
use rustc_ast::{AttrStyle, AttrVec, LocalKind, MacCall, MacCallStmt, MacStmtStyle};
2020
use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, HasAttrs, Local, Recovered, Stmt};
2121
use rustc_ast::{StmtKind, DUMMY_NODE_ID};
@@ -407,18 +407,24 @@ impl<'a> Parser<'a> {
407407

408408
fn check_let_else_init_trailing_brace(&self, init: &ast::Expr) {
409409
if let Some(trailing) = classify::expr_trailing_brace(init) {
410-
let sugg = match &trailing.kind {
411-
ExprKind::MacCall(mac) => errors::WrapInParentheses::MacroArgs {
412-
left: mac.args.dspan.open,
413-
right: mac.args.dspan.close,
414-
},
415-
_ => errors::WrapInParentheses::Expression {
416-
left: trailing.span.shrink_to_lo(),
417-
right: trailing.span.shrink_to_hi(),
418-
},
410+
let (span, sugg) = match trailing {
411+
TrailingBrace::MacCall(mac) => (
412+
mac.span(),
413+
errors::WrapInParentheses::MacroArgs {
414+
left: mac.args.dspan.open,
415+
right: mac.args.dspan.close,
416+
},
417+
),
418+
TrailingBrace::Expr(expr) => (
419+
expr.span,
420+
errors::WrapInParentheses::Expression {
421+
left: expr.span.shrink_to_lo(),
422+
right: expr.span.shrink_to_hi(),
423+
},
424+
),
419425
};
420426
self.dcx().emit_err(errors::InvalidCurlyInLetElse {
421-
span: trailing.span.with_lo(trailing.span.hi() - BytePos(1)),
427+
span: span.with_lo(span.hi() - BytePos(1)),
422428
sugg,
423429
});
424430
}

tests/ui/parser/bad-let-else-statement.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn t() {
205205
//~^ WARN irrefutable `let...else` pattern
206206
8
207207
} else {
208-
// FIXME: right curly brace `}` before `else` in a `let...else` statement not allowed
208+
//~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed
209209
return;
210210
};
211211
}

tests/ui/parser/bad-let-else-statement.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ help: use parentheses instead of braces for this macro
241241
LL | let bad = format_args! ("") else { return; };
242242
| ~ ~
243243

244+
error: right curly brace `}` before `else` in a `let...else` statement not allowed
245+
--> $DIR/bad-let-else-statement.rs:207:5
246+
|
247+
LL | } else {
248+
| ^
249+
|
250+
help: use parentheses instead of braces for this macro
251+
|
252+
LL ~ let foo = &std::ptr::null as &'static dyn std::ops::Fn() -> *const primitive! (
253+
LL |
254+
LL | 8
255+
LL ~ ) else {
256+
|
257+
244258
error: right curly brace `}` before `else` in a `let...else` statement not allowed
245259
--> $DIR/bad-let-else-statement.rs:190:25
246260
|
@@ -311,5 +325,5 @@ LL | | } else {
311325
= note: this pattern will always match, so the `else` clause is useless
312326
= help: consider removing the `else` clause
313327

314-
error: aborting due to 19 previous errors; 5 warnings emitted
328+
error: aborting due to 20 previous errors; 5 warnings emitted
315329

0 commit comments

Comments
 (0)