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 using Vec::<dyn Any>::new() in a const fn #84170

Open
droundy opened this issue Apr 13, 2021 · 4 comments
Open

error using Vec::<dyn Any>::new() in a const fn #84170

droundy opened this issue Apr 13, 2021 · 4 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@droundy
Copy link
Contributor

droundy commented Apr 13, 2021

I tried this code:

const fn newv() -> Vec<Box<dyn std::any::Any>> {
    Vec::new()
}

(Playground link)

I expected this to compile, since Vec::new() is a const function, and no dyn anything needs to be created.

Instead, this happened:

Compiling playground v0.0.1 (/playground)
error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
 --> src/lib.rs:1:20
  |
1 | const fn newv() -> Vec<Box<dyn std::any::Any>> {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
  = help: add `#![feature(const_fn)]` to the crate attributes to enable

Meta

rustc --version --verbose:

1.51.0 on the playground
@droundy droundy added the C-bug Category: This is a bug. label Apr 13, 2021
@estebank estebank added A-const-fn T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 13, 2021
@droundy
Copy link
Contributor Author

droundy commented Apr 15, 2021

BTW, if there is a workaround for this bug, I'd love to hear about it.

@Aaron1011
Copy link
Member

This is unrelated to Vec::new(): the program const fn new(val: &Vec<Box<dyn std::any::Any>>) {} also does not compile.

Rust is conservatively disallowing all non-Sized traits in const fns, until there's been more design work around the interaction of traits and const fn. See rust-lang/rfcs#2632 for more discussion.

@RalfJung
Copy link
Member

In particular, the very meaning of this type is up for discussion, so whether or not any values are being created is immaterial.

The open question is whether a dyn Trait returned by a const fn can have its methods called in another const fn, i.e., is there an implicit requirement here that the underlying type must implement const Trait or not? This has not been decided yet.

@larsfillmore
Copy link

BTW, if there is a workaround for this bug, I'd love to hear about it.

@droundy You've probably found a workaround for your specific case or do not need it any more, but I'll leave it here for others:

struct Wrapper {
    value: Box<dyn std::any::Any>,
}

const fn newv() -> Vec<Wrapper> {
    Vec::new()
}

@RalfJung RalfJung added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed A-const-fn labels Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants