Skip to content

Spurious type deduced when using ? #71685

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
yshui opened this issue Apr 29, 2020 · 5 comments
Closed

Spurious type deduced when using ? #71685

yshui opened this issue Apr 29, 2020 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@yshui
Copy link
Contributor

yshui commented Apr 29, 2020

I tried this code:

#![feature(associated_type_bounds)]
struct S;
fn fn1<I: IntoIterator<Item: AsRef<S>>>(_iter: I) -> Result<(), ()> {
    Ok(())
}

async fn fn2(s: &str) -> Result<(), ()> {
    let s = s.parse().map_err(|_| ())?; // <- Spurious type deduced
    fn1(std::iter::once(s)).map_err(|e| ())?;
    Ok(())
}

I expected to see this happen:

error[E0282]: type annotations needed, because type of s cannot be determined.

Instead, this happened:

error[E0277]: the trait bound '(): std::str::FromStr' is not satisfied

As if s is assigned type ()

@yshui yshui added the C-bug Category: This is a bug. label Apr 29, 2020
@jonas-schievink jonas-schievink added A-inference Area: Type inference F-associated_type_bounds `#![feature(associated_type_bounds)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2020
@yshui
Copy link
Contributor Author

yshui commented Apr 30, 2020

Looks like ? triggers the problem. Also this has nothing to do with associated type.

This has the problem:

fn fn2(s: &str) -> Result<(), ()> {
    let _ = s.parse().map_err(|_| ())?;
    Ok(())
}

But not this:

fn fn2(s: &str) {
    let _ = s.parse().unwrap();
}

@yshui yshui changed the title Spurious type deduced for associate type Spurious type deduced when using ? Apr 30, 2020
@yshui
Copy link
Contributor Author

yshui commented Apr 30, 2020

I think label "F-associated_type_bounds", "requires-nightly" should be removed. "A-diagnostics" should be added.

@jackh726
Copy link
Member

Minimal repro

fn fn2(s: &str) -> Result<(), ()> {
    let s = s.parse().map_err(|_| ())?;
    Ok(())
}

with

error[E0277]: the trait bound `(): FromStr` is not satisfied
 --> src/lib.rs:2:15
  |
2 |     let s = s.parse().map_err(|_| ())?;
  |               ^^^^^ the trait `FromStr` is not implemented for `()`

I agree with @yshui here: this is really just a diagnostics issue. s here is falling back to () since it's isn't used after being reassigned.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints and removed F-associated_type_bounds `#![feature(associated_type_bounds)]` requires-nightly This issue requires a nightly compiler in some way. labels Jan 28, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 2, 2021
Add some tests for associated-type-bounds issues

Closes rust-lang#38917
Closes rust-lang#40093
Closes rust-lang#43475
Closes rust-lang#63591

rust-lang#47897 is likely closable too, but it needs an MCVE
~~rust-lang#38917, rust-lang#40093, rust-lang#43475, rust-lang#47897 all are mislabeled and shouldn't have the `F-associated-type-bounds` label~~

~~rust-lang#71685 is also mislabeled as commented on in that thread~~
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Feb 2, 2021
@estebank
Copy link
Contributor

Current output:

error[E0277]: the trait bound `(): FromStr` is not satisfied
    --> f71.rs:2:15
     |
2    |     let s = s.parse().map_err(|_| ())?;
     |               ^^^^^ the trait `FromStr` is not implemented for `()`
     |
     = help: the following other types implement trait `FromStr`:
               bool
               char
               isize
               i8
               i16
               i32
               i64
               i128
             and 29 others
note: required by a bound in `core::str::<impl str>::parse`
    --> /home/gh-estebank/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:2310:21
     |
2310 |     pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
     |                     ^^^^^^^ required by this bound in `core::str::<impl str>::parse`

@estebank
Copy link
Contributor

This is a duplicate of #38508. Closing it in favor of the older one.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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