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

nightly-2021-10-01: the trait From<&str> is not implemented for std::string::String #89432

Closed
npmccallum opened this issue Oct 1, 2021 · 16 comments · Fixed by #91549
Closed
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@npmccallum
Copy link
Contributor

I tried this code:

pub struct Datum {
    /// The name of this datum.
    pub name: String,

    /// Whether the datum indicates support for the platform or not.
    pub pass: bool,

    /// Short additional information to display to the user.
    pub info: Option<String>,

    /// Longer explanatory message on how to resolve problems.
    pub mesg: Option<String>,
}
fn dev_kvm() -> super::Datum {
    let dev_kvm = std::path::Path::new("/dev/kvm");

    super::Datum {
        name: "Driver".into(),
        pass: dev_kvm.exists(),
        info: Some("/dev/kvm".into()),
        mesg: None,
    }
}

I expected to see this happen:

Successful compilation. This has compiled and worked for years until nightly-2021-10-01. Reverting to nightly-2021-09-30 produces a successful compilation.

Instead, this happened:

error[E0277]: the trait bound `std::string::String: From<&str>` is not satisfied
  --> src/backend/kvm/mod.rs:23:24
   |
23 |         name: "Driver".into(),
   |                        ^^^^ the trait `From<&str>` is not implemented for `std::string::String`
   |
   = note: required because of the requirements on the impl of `Into<std::string::String>` for `&str`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
19 | fn dev_kvm() -> super::Datum where std::string::String: From<&str> {
   |                              +++++++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `std::string::String: From<&str>` is not satisfied
  --> src/backend/kvm/mod.rs:25:20
   |
25 |         info: Some("/dev/kvm".into()),
   |               ---- ^^^^^^^^^^^^^^^^^ the trait `From<&str>` is not implemented for `std::string::String`
   |               |
   |               required by a bound introduced by this call
   |
   = note: required because of the requirements on the impl of `Into<std::string::String>` for `&str`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
19 | fn dev_kvm() -> super::Datum where std::string::String: From<&str> {
   |                              +++++++++++++++++++++++++++++++++++++

Meta

rustc --version --verbose:

rustc 1.57.0-nightly (aa7aca3b9 2021-09-30)
binary: rustc
commit-hash: aa7aca3b954131720df725e70d12e902eb3be1de
commit-date: 2021-09-30
host: x86_64-unknown-linux-gnu
release: 1.57.0-nightly
LLVM version: 13.0.0

cc @haraldh @wgwoods

@npmccallum npmccallum added the C-bug Category: This is a bug. label Oct 1, 2021
@npmccallum
Copy link
Contributor Author

This also appears to impact the use of major Rust libraries such as clap:

error[E0277]: the trait bound `std::string::String: From<&str>` is not satisfied
  --> src/main.rs:74:10
   |
74 | #[derive(StructOpt)]
   |          ^^^^^^^^^ the trait `From<&str>` is not implemented for `std::string::String`
   |
   = note: required because of the requirements on the impl of `Into<std::string::String>` for `&str`
note: required by a bound in `App::<'a, 'b>::new`
  --> /home/npmccallum/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/clap-2.33.3/src/app/mod.rs:80:19
   |
80 |     pub fn new<S: Into<String>>(n: S) -> Self {
   |                   ^^^^^^^^^^^^ required by this bound in `App::<'a, 'b>::new`
   = note: this error originates in the derive macro `StructOpt` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
75 | struct Info where std::string::String: From<&str> {}
   |             +++++++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `std::string::String: From<&str>` is not satisfied
  --> src/main.rs:78:10
   |
78 | #[derive(StructOpt)]
   |          ^^^^^^^^^ the trait `From<&str>` is not implemented for `std::string::String`
   |
   = note: required because of the requirements on the impl of `Into<std::string::String>` for `&str`
note: required by a bound in `App::<'a, 'b>::new`
  --> /home/npmccallum/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/clap-2.33.3/src/app/mod.rs:80:19
   |
80 |     pub fn new<S: Into<String>>(n: S) -> Self {
   |                   ^^^^^^^^^^^^ required by this bound in `App::<'a, 'b>::new`
   = note: this error originates in the derive macro `StructOpt` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
79 | struct Exec where std::string::String: From<&str> {
   |             +++++++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `std::string::String: From<&str>` is not satisfied
  --> src/main.rs:84:10
   |
84 | #[derive(StructOpt)]
   |          ^^^^^^^^^ the trait `From<&str>` is not implemented for `std::string::String`
   |
   = note: required because of the requirements on the impl of `Into<std::string::String>` for `&str`
note: required by a bound in `App::<'a, 'b>::new`
  --> /home/npmccallum/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/clap-2.33.3/src/app/mod.rs:80:19
   |
80 |     pub fn new<S: Into<String>>(n: S) -> Self {
   |                   ^^^^^^^^^^^^ required by this bound in `App::<'a, 'b>::new`
   = note: this error originates in the derive macro `StructOpt` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
86 | enum Options where std::string::String: From<&str> {
   |              +++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `enarx-keepldr` due to 12 previous errors

@ehuss
Copy link
Contributor

ehuss commented Oct 1, 2021

Thanks for the report! Unfortunately I am unable to reproduce with the given example. As written, it doesn't compile with the super:: path, but even with that removed, it seems to compile fine. Can you put together an example that fails to compile, perhaps linked to https://play.rust-lang.org/?

@npmccallum Or maybe you can put something together? I tried running the testsuite for structopt itself without any problems.

@npmccallum
Copy link
Contributor Author

@ehuss I tried to do the same and compilation succeeded. play.rust-lang.org doesn't have the affected nightly build. However, our open source code is failing to build. I'm not dismissing the possibility that rust may be catching some bad code on our part. But the compiler recommended fixes seem very odd...

npmccallum added a commit to enarx-archive/enarx-keepldr that referenced this issue Oct 1, 2021
This works around this bug:
rust-lang/rust#89432

Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
@ehuss
Copy link
Contributor

ehuss commented Oct 1, 2021

The playground appears to have the latest version to me:

image

I was able to bisect to #86853. cc @usbalbin

I might try to reduce it later today, but if someone could make a minimal reproduction beforehand, that would be very helpful.

@npmccallum
Copy link
Contributor Author

npmccallum commented Oct 1, 2021

@ehuss @usbalbin I spent some time trying to come up with a minimal reproduction, but I failed. I don't know exactly what is triggering this new compiler failure.

@usbalbin
Copy link
Contributor

usbalbin commented Oct 1, 2021

@oli-obk or @fee1-dead , any thoughts?

@cuviper
Copy link
Member

cuviper commented Oct 1, 2021

Minimized:

pub const fn foo() -> impl FnOnce() -> String {
    || "foo".into()
}

I guess it's forcing const trait evaluation within the closure that isn't really needed.

@cuviper
Copy link
Member

cuviper commented Oct 1, 2021

Well, mine can't be all of it, because not all of the errors are from const functions, and my reproducer doesn't even work if you change to || String::new() because impl Trait and fn pointers aren't allowed. But maybe this is still a useful breadcrumb.

@cuviper
Copy link
Member

cuviper commented Oct 1, 2021

Here's a better one, which does work on beta:

const FOO: fn() -> String = || "foo".into();

pub fn bar() -> fn() -> String {
    || "bar".into()
}

The presence of FOO creates errors in both, though bar() alone works fine.

error[E0277]: the trait bound `String: From<&str>` is not satisfied
 --> src/lib.rs:1:38
  |
1 | const FOO: fn() -> String = || "foo".into();
  |                                      ^^^^ the trait `From<&str>` is not implemented for `String`
  |
  = note: required because of the requirements on the impl of `Into<String>` for `&str`

error[E0277]: the trait bound `String: From<&str>` is not satisfied
 --> src/lib.rs:4:14
  |
4 |     || "bar".into()
  |              ^^^^ the trait `From<&str>` is not implemented for `String`
  |
  = note: required because of the requirements on the impl of `Into<String>` for `&str`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
  |
3 | pub fn bar() -> fn() -> String where String: From<&str> {
  |                                ++++++++++++++++++++++++

@Urgau
Copy link
Member

Urgau commented Oct 1, 2021

Here is a godbolt for the first minimized code that produce the same error describe in this issue but doesn't work on stable or beta.
And here is the second godbolt for the second minimized code that's works on stable and beta but not on nightly.

@rustbot label +regression-from-stable-to-nightly

@rustbot rustbot added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Oct 1, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Oct 1, 2021

Short term we should revert #86853.

Longer term I believe this is due to global caching of predicate evaluations. We'll need to think about how const traits play into that, since their successful evaluation depends on the context's constness, which is obviously not global

@eddyb
Copy link
Member

eddyb commented Oct 1, 2021

on the context's constness, which is obviously not global

Shouldn't it be added to ParamEnv then? I thought that was the new policy: no more context outside ParamEnv.

@usbalbin
Copy link
Contributor

usbalbin commented Oct 1, 2021

Revert-PR created #89450

usbalbin added a commit to usbalbin/rust that referenced this issue Oct 1, 2021
Co-authored-by: Josh Stone <cuviper@gmail.com>
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 2, 2021
@apiraino
Copy link
Contributor

apiraino commented Oct 2, 2021

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-critical

@rustbot rustbot added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Oct 2, 2021
@oli-obk oli-obk added A-const-fn and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. P-critical Critical priority labels Oct 2, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2021

The revert has landed, we'll unrevert once we have the param env fix

wgwoods added a commit to wgwoods/enarx-keepldr that referenced this issue Oct 4, 2021
Rust nightly started giving  us compilation problems, so pin to a
known-good version until it's fixed/reverted.

See rust-lang/rust#89432 for details.

Signed-off-by: Will Woods <will@profian.com>
wgwoods added a commit to wgwoods/enarx that referenced this issue Oct 5, 2021
Rust nightly started giving  us compilation problems, so pin to a
known-good version until it's fixed/reverted.

See rust-lang/rust#89432 for details.

Signed-off-by: Will Woods <will@profian.com>
@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 12, 2021
@fee1-dead
Copy link
Member

fee1-dead commented Dec 3, 2021

Here is another MCVE which is what seems to cause the error: playground

#![feature(const_trait_impl)]
#![feature(const_fn_trait_bounds)]

trait Convert<T> {
    fn to(self) -> T;
}

impl<A, B> const Convert<B> for A where B: ~const From<A> {
    fn to(self) -> B {
        B::from(self)
    }
}

const FOO: fn() -> String = || "foo".to();

@fee1-dead fee1-dead added the F-const_trait_impl `#![feature(const_trait_impl)]` label Dec 3, 2021
@bors bors closed this as completed in 22f8bde Dec 13, 2021
flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 17, 2021
Eliminate ConstnessAnd again

Closes rust-lang#91489.
Closes rust-lang#89432.

Reverts rust-lang#91491.
Reverts rust-lang#89450.

r? `@spastorino`
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label 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. F-const_trait_impl `#![feature(const_trait_impl)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.