Skip to content

Contracts: diagnostic message for mismatched function return type points to ensures clause #149742

@dawidl022

Description

@dawidl022

When a function is annotated with a contracts::ensures clause, the compiler emits confusing error messages when the return expression's type mismatches the function's declared type. The messages point at the ensures clause and the internal contract_check_ensures function, instead of the offending expression in the function body.

I'm unsure as to whether the fix is easy (e.g. ensuring the right spans are used in the right places during lowering), or would require rethinking the lowering apporach.

Minimal reproducible examples:

  • Implicit return expression
    #[core::contracts::ensures(|_| { true })]
    fn foo() -> u32 {
        true
    }
  • Explicit "early" return
    #[core::contracts::ensures(|_| { true })]
    fn foo() -> u32 {
        if true {
            return true
        }
        0
    }

Both these examples provide an identical output on the main branch:

error[E0308]: mismatched types
    --> src/main.rs:3:1
     |
   3 | #[core::contracts::ensures(|_| { true })]
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     | |
     | expected `u32`, found `bool`
     | arguments to this function are incorrect
     |
note: function defined here
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics/mod.rs:2709:14
     |
2709 | pub const fn contract_check_ensures<C: Fn(&Ret) -> bool + Copy, Ret>(
     |              ^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
 --> src/main.rs:3:1
  |
3 | #[core::contracts::ensures(|_| { true })]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `bool`
4 | fn foo() -> u32 {
  |             --- expected `u32` because of return type

For more information about this error, try `rustc --explain E0308`.

The former example demonstrates a regression introduced in #144438, whereas the latter did not work correctly even before that change, giving the following output:

error[E0308]: mismatched types
  --> /Users/dawidl022/Development/rust/tests/ui/contracts/return-early-span.rs:10:16
   |
LL | #[core::contracts::ensures(|_| { true })]
   | ----------------------------------------- arguments to this function are incorrect
...
LL |         return true;
   |                ^^^^ expected `u32`, found `bool`
   |
note: function defined here
  --> /rustc/FAKE_PREFIX/library/core/src/intrinsics/mod.rs:2692:14

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

The expected behaviour is that the presence of contract annotation on a function do not affect the emitted error messages for errors in the function body. E.g. for the examples above respectively:

error[E0308]: mismatched types
 --> src/main.rs:4:5
  |
3 | fn foo() -> u32 {
  |             --- expected `u32` because of return type
4 |     true
  |     ^^^^ expected `u32`, found `bool`

For more information about this error, try `rustc --explain E0308`.
error[E0308]: mismatched types
 --> src/main.rs:5:16
  |
3 | fn foo() -> u32 {
  |             --- expected `u32` because of return type
4 |     if true {
5 |         return true
  |                ^^^^ expected `u32`, found `bool`

For more information about this error, try `rustc --explain E0308`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions