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 · 2 comments

Comments

Projects
None yet
4 participants
@3442853561
Copy link

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.

@ExpHP

This comment has been minimized.

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

This comment has been minimized.

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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment