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

Error when autoderef of smart pointer occurs in const fn is unclear #91496

Closed
SNCPlay42 opened this issue Dec 3, 2021 · 2 comments
Closed

Error when autoderef of smart pointer occurs in const fn is unclear #91496

SNCPlay42 opened this issue Dec 3, 2021 · 2 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SNCPlay42
Copy link
Contributor

Given the following code:

use std::ops::Deref;

struct Foo;

impl Foo {
    const fn foo(&self) {}
}

struct SmartPointer(Foo);

impl Deref for SmartPointer {
    type Target = Foo;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

pub const fn bar() {
    Foo.foo(); // fine
    SmartPointer(Foo).foo(); // fails since we can't constly deref
}

The current output is:

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  --> src/lib.rs:21:5
   |
21 |     SmartPointer(Foo).foo(); // fails since we can't constly deref
   |     ^^^^^^^^^^^^^^^^^^^^^^^

It's unclear from the diagnostic that the problem is not the call of foo itself (which is a const fn) but that SmartPointer would have to be dereferenced.

Ideally the output should look like:

error[E0015]: `SmartPointer` cannot be dereferenced in constant functions
  --> src/lib.rs:21:5
   |
21 |     SmartPointer(Foo).foo(); // fails since we can't constly deref
   |     ^^^^^^^^^^^^^^^^^^^^^^^ dereference occurs due to method call
   |
   = note: operators in constant functions are limited to intrinsic operations

Perhaps more generally, the compiler should avoid referring to non-intrinsic operators as "calls" in const errors, or explain that it means calling the operator method. This is usually less of a problem because an operator appears in the span.

@rustbot label A-const-fn

@SNCPlay42 SNCPlay42 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 3, 2021
@rustbot rustbot added the A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. label Dec 3, 2021
@inquisitivecrystal inquisitivecrystal added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-const_trait_impl `#![feature(const_trait_impl)]` and removed F-const_trait_impl `#![feature(const_trait_impl)]` labels Dec 3, 2021
@djc
Copy link
Contributor

djc commented Feb 23, 2022

I think this is the same problem:

match &self.0 {
    Cow::Borrowed(s) => s,
    Cow::Owned(s) => &*s,
}

Fails with

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  --> opentelemetry-api/src/common.rs:61:9
   |
61 | /         match &self.0 {
62 | |             Cow::Borrowed(s) => s,
63 | |             Cow::Owned(s) => &*s,
64 | |         }
   | |_________^

Despite there clearly being no calls in this function.

@SNCPlay42
Copy link
Contributor Author

Output on current nightly, after #90532:

error[E0015]: cannot perform deref coercion on `SmartPointer` in constant functions
  --> src/lib.rs:21:5
   |
21 |     SmartPointer(Foo).foo(); // fails since we can't constly deref
   |     ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: attempting to deref into `Foo`
note: deref defined here
  --> src/lib.rs:12:5
   |
12 |     type Target = Foo;
   |     ^^^^^^^^^^^^^^^^^^
note: impl defined here, but it is not `const`
  --> src/lib.rs:11:1
   |
11 | impl Deref for SmartPointer {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

Output from #91496 (comment), (as reproduced like this) has improved as well, although the span is still a bit confusing:

error[E0015]: cannot perform deref coercion on `String` in constant functions
    --> src/lib.rs:7:9
     |
7    | /         match &self.0 {
8    | |             Cow::Borrowed(s) => s,
9    | |             Cow::Owned(s) => &*s,
10   | |         }
     | |_________^
     |
     = note: attempting to deref into `str`
note: impl defined here, but it is not `const`
     = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

At any rate, I'm closing this as fixed/in favor of the broader #92380.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants