Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Tracking Issue for RFC 213: Default Type Parameter Fallback #27336

Closed

Description

@jroesch

View all comments

2024-07-15: This feature (default_type_parameter_fallback) is slated for removal, but that is currently blocked on too many crater regressions.


EDIT: this issue has been stalled on disagreements about how to handle a nasty problem found during implementation. See the internals thread where this was detailed and discussed.


This is a tracking issue for RFC 213.

The initial implementation of this feature has landed.

cc @nikomatsakis

Activity

  1. self-assigned this
    on Jul 27, 2015
  2. added
    B-RFC-approved
    on Jul 29, 2015
  3. added
    T-lang
    on Aug 11, 2015
  4. added
    B-unstable
    on Aug 14, 2015
  5. bluss commented on Nov 20, 2015

    @bluss
    Contributor

    What is the status of this?

  6. pnkfelix commented on Dec 1, 2015

    @pnkfelix
    Contributor

    @bluss it is still feature-gated, AFAICT. See e.g. the discussion on PR #26870 , or just look at this playpen

    I am not sure what the planned schedule is for unfeature-gating it.

    nominating for discussion.

  7. mahkoh commented on Dec 11, 2015

    @mahkoh
    Contributor

    This doesn't seem to be working properly.

    #![crate_type = "lib"]
    #![feature(default_type_parameter_fallback)]
    
    trait A<T = Self> {
        fn a(t: &T) -> Self;
    }
    
    trait B<T = Self> {
        fn b(&self) -> T;
    }
    
    impl<U, T = U> B<T> for U
        where T: A<U>
    {
        fn b(&self) -> T {
            T::a(self)
        }
    }
    
    struct X(u8);
    
    impl A for X {
        fn a(x: &X) -> X {
            X(x.0)
        }
    }
    
    fn f(x: &X) {
        x.b(); // ok
    }
    
    fn g(x: &X) {
        let x = x.b();
        x.0; // error: the type of this value must be known in this context
    }
  8. jroesch commented on Dec 15, 2015

    @jroesch
    ContributorAuthor

    @mahkoh there is a necessary patch that hasn't gotten rebased since I stopped my summer internship. I've been unfortunately busy with real life stuff, looks like @nikomatsakis has plans for landing a slightly different version according to a recent post of his on the corresponding documentation issue for this feature.

  9. bluss commented on Feb 22, 2016

    @bluss
    Contributor

    @nikomatsakis I know the lang team didn't see any future in this feature, will you put that on record in the issue 😄?

    One example where this feature seems to be the only way out is the following concrete example of API evolution in libstd.

    Option<T> implements PartialEq today, but we would like to extend it to PartialEq<Option<U>> where T: PartialEq<U>. It appears this feature can solve the type inference regressions that would otherwise occur (and might block us from doing this oft-requested improvement of Option).

  10. nikomatsakis commented on Feb 23, 2016

    @nikomatsakis
    Contributor

    @bluss I HAVE been dubious of this feature, but I'm been slowly reconsidering. @aturon is supposed to be doing some exploration of this whole space and writing up some detailed thoughts. I actually started rebasing @jroesch's dead branch to implement the desired semantics and making some progress there too, but I've been distracted.

    One advantage of finishing up the impl is that it would let us experiment with extensions like the one you describe to see how backwards compatible they truly are -- one problem with fallback is that it is not ACTUALLY backwards compatible, because of the possibility of competing incompatible fallbacks.

  11. nikomatsakis commented on Feb 23, 2016

    @nikomatsakis
    Contributor

    That said I still have my doubts :)

  12. 79 remaining items

  13. lcnr commented on Jul 15, 2024

    @lcnr
    Contributor

    I think we should completely remove the feature gate. It's currently only partially implemented and requires some significant design work before we re-add it afaict.

  14. nikomatsakis commented on Jul 15, 2024

    @nikomatsakis
    Contributor
  15. RalfJung commented on Jul 15, 2024

    @RalfJung
    Member

    Sadly that's not really possible currently, typemap uses this feature...

  16. RalfJung commented on Jul 15, 2024

    @RalfJung
    Member

    I will reopen the issue then -- as long as the feature gate exists, the tracking issue it points to should remain open.

  17. added 5 commits that reference this issue on Aug 4, 2024
  18. added a commit that references this issue on Aug 5, 2024
  19. nik-rev commented on Mar 8, 2026

    @nik-rev
    Contributor

    I found a hack that allows us to pretend that default type parameters affect inference in some circumstances

    Given this:

    use std::io;
    
    struct Hook<Sink = io::Stderr>(Sink);
    
    impl Default for Hook {
        fn default() -> Self {
            Self(io::stderr())
        }
    }
    
    impl Default for Hook<io::Stdout> {
        fn default() -> Self {
            Self(io::stdout())
        }
    }

    I want users to be able to write this:

    Hook::default();

    But that won't work, the default type parameter Sink = io::Stderr does not affect type inference:

    error[E0283]: type annotations needed
      --> src/main.rs:18:5
       |
    18 |     Hook::default();
       |     ^^^^ cannot infer type for struct `main::Hook<_>`
       |
    note: multiple `impl`s satisfying `main::Hook<_>: Default` found
      --> src/main.rs:6:5
       |
     6 |     impl Default for Hook {
       |     ^^^^^^^^^^^^^^^^^^^^^
    ...
    12 |     impl Default for Hook<io::Stdout> {
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    The solution is to add a method called default() that just calls Default::default():

    impl Hook {
        pub fn default() -> Self {
            Default::default()
        }
    }

    That causes the expression Hook::default() to really call Hook::<io::Stderr>::default(), which calls <Hook<io::Stderr> as Default>::default()

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

Metadata

Metadata

Assignees

Labels

B-RFC-approvedB-RFC-implementedB-unstableC-tracking-issueF-default_type_parameter_fallbackT-lang

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions