Skip to content

Commit

Permalink
Rollup merge of rust-lang#104147 - WaffleLapkin:don't-compare-ptr-add…
Browse files Browse the repository at this point in the history
…resses-in-parser-, r=compiler-errors

Remove an address comparison from the parser

Originally this check was added in rust-lang#68985, as suggested by rust-lang@940f657#r376850175. I don't think that this address check is a robust way of making parser more robust.

This code is also extensively tested by [`ui/parser/issues/issue-35813-postfix-after-cast.rs`](https://github.com/rust-lang/rust/blob/57d3c58ed6e0faf89a62411f96c000ffc9fd3937/src/test/ui/parser/issues/issue-35813-postfix-after-cast.rs).

_Replaces #103700_

r? `@compiler-errors`
  • Loading branch information
GuillaumeGomez committed Nov 8, 2022
2 parents ed38562 + 268ea35 commit e3c98a8
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,16 +833,11 @@ impl<'a> Parser<'a> {
("cast", None)
};

// Save the memory location of expr before parsing any following postfix operators.
// This will be compared with the memory location of the output expression.
// If they different we can assume we parsed another expression because the existing expression is not reallocated.
let addr_before = &*cast_expr as *const _ as usize;
let with_postfix = self.parse_dot_or_call_expr_with_(cast_expr, span)?;
let changed = addr_before != &*with_postfix as *const _ as usize;

// Check if an illegal postfix operator has been added after the cast.
// If the resulting expression is not a cast, or has a different memory location, it is an illegal postfix operator.
if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _)) || changed {
// If the resulting expression is not a cast, it is an illegal postfix operator.
if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _)) {
let msg = format!(
"{cast_kind} cannot be followed by {}",
match with_postfix.kind {
Expand Down

0 comments on commit e3c98a8

Please sign in to comment.