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

impl Trait feature is not make code easy #47348

Open
3442853561 opened this issue Jan 11, 2018 · 3 comments
Open

impl Trait feature is not make code easy #47348

3442853561 opened this issue Jan 11, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@3442853561
Copy link
Contributor

3442853561 commented Jan 11, 2018

#![feature(conservative_impl_trait, universal_impl_trait)]

use std::ops::Sub;

trait Trait: Sub + Copy {}

impl Trait for i32{}

fn test0(foo: impl Trait) -> <impl Trait as std::ops::Sub>::Output {
    foo - foo
}

fn test1<T: Sub + Copy>(foo: T) -> T::Output {
    foo - foo
}

fn main() {
    let foo = test0(1);
    let bar = test1(1);
    println!("{:?}", bar);
}

Only the bar can be debug.

@kennytm kennytm added the A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. label Jan 11, 2018
@ExpHP
Copy link
Contributor

ExpHP commented Jan 23, 2018

fn test0(foo: impl Trait) -> <impl Trait as std::ops::Sub>::Output {
    foo - foo
}

Should this even compile? I would have figured that the impl Trait in the output type should be considered ambiguous.

@ExpHP
Copy link
Contributor

ExpHP commented Feb 28, 2018

#48084 makes the snippet no longer compile.

   Compiling playground v0.0.1 (file:///playground)
error[E0667]: `impl Trait` is not allowed in path parameters
 --> src/main.rs:9:31
  |
9 | fn test0(foo: impl Trait) -> <impl Trait as std::ops::Sub>::Output {
  |                               ^^^^^^^^^^

That said, I can see what originally led the author to write this signature; observe the error message for an incorrect return type:

   Compiling playground v0.0.1 (file:///playground)
error[E0308]: mismatched types
  --> src/main.rs:10:5
   |
9  | fn test0(foo: impl Trait)  {
   |                            - possibly return type missing here?
10 |     foo - foo
   |     ^^^^^^^^^ expected (), found associated type
   |
   = note: expected type `()`
              found type `<impl Trait as std::ops::Sub>::Output`

@XAMPPRocky XAMPPRocky added T-lang Relevant to the language team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Apr 10, 2018
@jyn514
Copy link
Member

jyn514 commented Jul 7, 2021

Triage: the error message for foo0 is now

error[E0308]: mismatched types
  --> src/main.rs:10:5
   |
9  | fn test0(foo: impl Trait) {
   |                           - possibly return type missing here?
10 |     foo - foo
   |     ^^^^^^^^^ expected `()`, found associated type
   |
   = note:    expected unit type `()`
           found associated type `<impl Trait as Sub>::Output`
   = help: consider constraining the associated type `<impl Trait as Sub>::Output` to `()`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

Maybe that's helpful enough? This definitely doesn't seem like a bug though, I'm going to change it to a diagnostic request.

@jyn514 jyn514 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. and removed C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. 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

5 participants