Skip to content
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

try_validation unconditionally adds semicolons #6242

Closed
jyn514 opened this issue Oct 27, 2020 · 1 comment · Fixed by #6278
Closed

try_validation unconditionally adds semicolons #6242

jyn514 opened this issue Oct 27, 2020 · 1 comment · Fixed by #6278
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@jyn514
Copy link
Member

jyn514 commented Oct 27, 2020

I tried this code (https://github.com/rust-lang/rust/blob/0da6d42f297642a60f2640ec313b879b376b9ad8/compiler/rustc_mir/src/interpret/validity.rs#L78-L96):

macro_rules! try_validation {
    ($e:expr, $where:expr,
     $( $( $p:pat )|+ => { $( $what_fmt:expr ),+ } $( expected { $( $expected_fmt:expr ),+ } )? ),+ $(,)?
    ) => {{
        match $e {
            Ok(x) => x,
            // We catch the error and turn it into a validation failure. We are okay with
            // allocation here as this can only slow down builds that fail anyway.
            $( $( Err(InterpErrorInfo { kind: $p, .. }) )|+ =>
                throw_validation_failure!(
                    $where,
                    { $( $what_fmt ),+ } $( expected { $( $expected_fmt ),+ } )?
                ),
            )+
            #[allow(unreachable_patterns)]
            Err(e) => Err::<!, _>(e)?,
        }
    }};
}

I expected to see this happen: The Err(e)? is replaced with return Err(e).

Instead, this happened: It's replaced with return Err(e);, causing a syntax error since this is an expression context.

error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
   --> compiler/rustc_mir/src/interpret/validity.rs:97:18
    |
78  |  / macro_rules! try_validation {
79  |  |     ($e:expr, $where:expr,
80  |  |      $( $( $p:pat )|+ => { $( $what_fmt:expr ),+ } $( expected { $( $expected_fmt:expr ),+ } )? ),+ $(,)?
81  |  |     ) => {{
...    |
93  |  |             Err(e) => return Err(try_validation!(
    |  |__________________________________-
94  | ||                     self.ecx.memory.read_bytes(mplace.ptr, Size::from_bytes(len)),
95  | ||                     self.path,
96  | ||                     err_ub!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" },
97  | ||                 );),
    | ||                 -^ expected one of `)`, `,`, `.`, `?`, or an operator
    | ||_________________|
    |  |                 in this macro invocation (#2)
98  |  |         }
99  |  |     }};
100 |  | }
    |  | -
    |  | |
    |  |_in this expansion of `try_validation!` (#1)
    |    in this expansion of `try_validation!` (#2)
...
341 | /                  try_validation!(
342 | |                      self.ecx.read_size_and_align_from_vtable(vtable),
343 | |                      self.path,
344 | |                      err_unsup!(ReadPointerAsBytes) => { "invalid size or align in vtable" },
345 | |                  );
    | |___________________- in this macro invocation (#1)

warning: returning an `Err(_)` with the `?` operator
   --> compiler/rustc_mir/src/interpret/validity.rs:93:23
    |
78  | / macro_rules! try_validation {
79  | |     ($e:expr, $where:expr,
80  | |      $( $( $p:pat )|+ => { $( $what_fmt:expr ),+ } $( expected { $( $expected_fmt:expr ),+ } )? ),+ $(,)?
81  | |     ) => {{
...   |
93  | |             Err(e) => Err::<!, _>(e)?,
    | |                       ^^^^^^^^^^^^^^^
94  | |         }
95  | |     }};
96  | | }
    | |_- in this expansion of `try_validation!`
...
310 | /                 try_validation!(
311 | |                     self.ecx.memory.check_ptr_access_align(
312 | |                         vtable,
313 | |                         3 * self.ecx.tcx.data_layout.pointer_size, // drop, size, align
...   |
325 | |                         { "too small vtable" },
326 | |                 );
    | |__________________- in this macro invocation
    |
    = note: `#[warn(clippy::try_err)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#try_err
help: try this
    |
93  |             Err(e) => return Err(try_validation!(
94  |                     self.ecx.memory.check_ptr_access_align(
95  |                         vtable,
96  |                         3 * self.ecx.tcx.data_layout.pointer_size, // drop, size, align
97  |                         Some(self.ecx.tcx.data_layout.pointer_align.abi),
98  |                         CheckInAllocMsg::InboundsTest,
  ...

Meta

  • cargo clippy -V: clippy 0.0.212 (ffa2e7a 2020-10-24)
@jyn514 jyn514 added the C-bug Category: Clippy is not doing the correct thing label Oct 27, 2020
@jyn514
Copy link
Member Author

jyn514 commented Oct 27, 2020

Actually I think this is the same issue as #6234, clippy is expanding e to $e. But that seems really weird because e is a variable and not a macro parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant