Skip to content

impl Trait feature is not make code easy #47348

Closed as not planned
Closed as not planned

Description

@3442853561
#![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.

Activity

  1. added
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    on Jan 11, 2018
  2. ExpHP commented on Jan 23, 2018

    @ExpHP
    Contributor
    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.

  3. ExpHP commented on Feb 28, 2018

    @ExpHP
    Contributor

    #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`
    
  4. added
    T-langRelevant to the language team
    C-bugCategory: This is a bug.
    on Apr 10, 2018
  5. jyn514 commented on Jul 7, 2021

    @jyn514
    Member

    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.

  6. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    C-bugCategory: This is a bug.
    T-langRelevant to the language team
    on Jul 7, 2021
  7. Dylan-DPC commented on Dec 11, 2024

    @Dylan-DPC
    Member

    Current output: playground link

    error[E0562]: `impl Trait` is not allowed in paths
     --> src/main.rs:8:31
      |
    8 | fn test0(foo: impl Trait) -> <impl Trait as std::ops::Sub>::Output {
      |                               ^^^^^^^^^^
      |
      = note: `impl Trait` is only allowed in arguments and return types of functions and methods
    
  8. Dylan-DPC commented on Dec 11, 2024

    @Dylan-DPC
    Member

    Closing this issue as this code doesn't compile any more as intended, and maybe the diagnostic should be improved but i feel that's better as a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions