Skip to content

const trait checks allow calling non-const methods #88155

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Tracked by #67792
lcnr opened this issue Aug 19, 2021 · 0 comments · Fixed by #88328
Closed
Tracked by #67792

const trait checks allow calling non-const methods #88155

lcnr opened this issue Aug 19, 2021 · 0 comments · Fixed by #88328
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]`

Comments

@lcnr
Copy link
Contributor

lcnr commented Aug 19, 2021

cc https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/const.20impl.20trait.20const.20check

This requires 2 different crates:

// crate-a
#![feature(const_fn_trait_bound)]
#![feature(const_trait_impl)]
#![feature(const_trait_bound_opt_out)]
#![allow(incomplete_features)]

pub trait A {
    fn assoc() -> bool;
}

pub const fn foo<T: ?const A>() -> bool {
    T::assoc()
}

// crate-b
#![feature(const_fn_trait_bound)]
#![feature(const_trait_impl)]
#![feature(const_trait_bound_opt_out)]
#![allow(incomplete_features)]
extern crate a;

struct Foo;

impl a::A for Foo {
    fn assoc() -> bool {
        println!("hey ho");
        true
    }
}

static F: bool =  a::foo::<Foo>();

fn main() {
    println!("{}", F);
}

results in

error[E0080]: could not evaluate static initializer
  --> /home/lcnr/wtf/a/src/lib.rs:11:5
   |
11 |     T::assoc()
   |     ^^^^^^^^^^
   |     |
   |     calling non-const function `<Foo as A>::assoc`
   |     inside `foo::<Foo>` at /home/lcnr/wtf/a/src/lib.rs:11:5
   |
  ::: src/main.rs:15:19
   |
15 | static F: bool =  a::foo::<Foo>();
   |                   --------------- inside `F` at src/main.rs:15:19

relevant code

if !permitted {
// if trait's impls are all const, permit the call.
let mut const_impls = true;
tcx.for_each_relevant_impl(trait_id, substs.type_at(0), |imp| {
if const_impls {
if let hir::Constness::NotConst = tcx.impl_constness(imp) {
const_impls = false;
}
}
});
if const_impls {
permitted = true;
}
}

with #86986 we don't find any impl for PartialEq which might apply here which causes us to incorrectly accept https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2632-const-trait-impl/call-generic-method-fail.rs

@lcnr lcnr added C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` labels Aug 19, 2021
@fee1-dead fee1-dead mentioned this issue Aug 25, 2021
10 tasks
fee1-dead added a commit to fee1-dead-contrib/rust that referenced this issue Aug 27, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 27, 2021
Introduce `~const`

 - [x] Removed `?const` and change uses of `?const`
 - [x] Added `~const` to the AST. It is gated behind const_trait_impl.
 - [x] Validate `~const` in ast_validation.
 - [x] Update UI Tests
 - [x] Add enum `BoundConstness` (With variants `NotConst` and
 `ConstIfConst` allowing future extensions)
 - [x] Adjust trait selection and pre-existing code to use `BoundConstness`.
 - [ ] Optional steps for this PR
      - [x] Fix rust-lang#88155
      - [x] ~~Do something with constness bounds in chalk~~ Must be done to rust-lang/chalk (just tried to refactor, there are a lot of errors to resolve :( )
      - [ ] Adjust Error messages for `~const` bounds that can't be satisfied.

r? `@oli-obk`
@bors bors closed this as completed in 8660832 Aug 28, 2021
calebcartwright pushed a commit to calebcartwright/rust that referenced this issue Oct 20, 2021
 - [x] Removed `?const` and change uses of `?const`
 - [x] Added `~const` to the AST. It is gated behind const_trait_impl.
 - [x] Validate `~const` in ast_validation.
 - [ ] Add enum `BoundConstness` to the HIR. (With variants `NotConst` and
 `ConstIfConst` allowing future extensions)
 - [ ] Adjust trait selection and pre-existing code to use `BoundConstness`.
 - [ ] Optional steps (*for this PR, obviously*)
      - [ ] Fix rust-lang#88155
      - [ ] Do something with constness bounds in chalk
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant