Skip to content

Typecheck fails when providing explicit types instead of GAT #87803

Open
@ear7h

Description

@ear7h

I tried to compile this code:

#![feature(generic_associated_types)]

trait Scanner {
    type Input<'a>;
    type Token<'a>;
    
    fn scan<'a>(&mut self, i : Self::Input<'a>) -> Self::Token<'a>;
}

struct IdScanner();

impl Scanner for IdScanner {
    type Input<'a> = &'a str;
    type Token<'a> = &'a str;
    
    fn scan<'a>(&mut self, s : &'a str) -> &'a str {
        s
    }
}

I expected to see this happen:
The code compile successfully

Instead, this happened:
Rust could not match the lifetimes.

error[E0195]: lifetime parameters or bounds on method `scan` do not match the trait declaration
  --> src/lib.rs:16:12
   |
7  |     fn scan<'a>(&mut self, i : Self::Input<'a>) -> Self::Token<'a>;
   |            ---- lifetimes in impl do not match this method in trait
...
16 |     fn scan<'a>(&mut self, s : &'a str) -> &'a str {
   |            ^^^^ lifetimes do not match method in trait

For more information about this error, try `rustc --explain E0195`.

Changing the impl method to:

fn scan<'a>(&mut self, s : Self::Input<'a>) -> Self::Token<'a> {

Does compile successfully.

This seems somewhat related to #79207 and possibly in scope of #85499

Meta

I tried this on nightly as well as with the latest code from #85499 this commit
nightly:

rustc 1.56.0-nightly (492723897 2021-07-29)                                           
binary: rustc                                                                         
commit-hash: 492723897e9b4db6701b3a75b72618d08a7d5319                                 
commit-date: 2021-07-29                                                               
host: x86_64-apple-darwin                                                             
release: 1.56.0-nightly                                                               
LLVM version: 12.0.1

from jackh726:

rustc 1.55.0-dev                                                                      
binary: rustc                                                                         
commit-hash: unknown                                                                  
commit-date: unknown                                                                  
host: x86_64-apple-darwin                                                             
release: 1.55.0-dev                                                                   
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)GATs-triagedIssues using the `generic_associated_types` feature that have been triagedS-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions