- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
          Suggest adding Fn bound when calling a generic parameter
          #144193
        
          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
          
     Open
      
      
            Kobzol
  wants to merge
  1
  commit into
  rust-lang:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Kobzol:generic-param-fn-bound-help
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +154
        
        
          −6
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| fn return_type<T>(t: T) { | ||
| let x: u32 = t(1); | ||
| //~^ ERROR: expected function, found `T` [E0618] | ||
| } | ||
| 
     | 
||
| fn unknown_return_type<T>(t: T) { | ||
| let x = t(); | ||
| //~^ ERROR: expected function, found `T` [E0618] | ||
| } | ||
| 
     | 
||
| fn nested_return_type<T>(t: Vec<T>) { | ||
| t(); | ||
| //~^ ERROR: expected function, found `Vec<T>` [E0618] | ||
| } | ||
| 
     | 
||
| fn no_return_type<T>(t: T) { | ||
| t(1, 2, true); | ||
| //~^ ERROR: expected function, found `T` [E0618] | ||
| } | ||
| 
     | 
||
| fn existing_bound<T: Copy>(t: T) { | ||
| t(false); | ||
| //~^ ERROR: expected function, found `T` [E0618] | ||
| } | ||
| 
     | 
||
| fn main() {} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| error[E0618]: expected function, found `T` | ||
| --> $DIR/suggest-call-on-generic-param.rs:2:18 | ||
| | | ||
| LL | fn return_type<T>(t: T) { | ||
| | - `t` has type `T` | ||
| LL | let x: u32 = t(1); | ||
| | ^--- | ||
| | | | ||
| | call expression requires function | ||
| | | ||
| help: consider restricting type parameter `T` with trait `Fn` | ||
| | | ||
| LL | fn return_type<T: Fn(i32) -> u32>(t: T) { | ||
| | ++++++++++++++++ | ||
| 
     | 
||
| error[E0618]: expected function, found `T` | ||
| --> $DIR/suggest-call-on-generic-param.rs:7:13 | ||
| | | ||
| LL | fn unknown_return_type<T>(t: T) { | ||
| | - `t` has type `T` | ||
| LL | let x = t(); | ||
| | ^-- | ||
| | | | ||
| | call expression requires function | ||
| | | ||
| help: consider restricting type parameter `T` with trait `Fn` | ||
| | | ||
| LL | fn unknown_return_type<T: Fn()>(t: T) { | ||
| | ++++++ | ||
| 
     | 
||
| error[E0618]: expected function, found `Vec<T>` | ||
| --> $DIR/suggest-call-on-generic-param.rs:12:5 | ||
| | | ||
| LL | fn nested_return_type<T>(t: Vec<T>) { | ||
| | - `t` has type `Vec<T>` | ||
| LL | t(); | ||
| | ^-- | ||
| | | | ||
| | call expression requires function | ||
| | | ||
| help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement | ||
| | | ||
| LL | fn nested_return_type<T>(t: Vec<T>) where Vec<T>: Fn() { | ||
| | ++++++++++++++++++ | ||
| 
     | 
||
| error[E0618]: expected function, found `T` | ||
| --> $DIR/suggest-call-on-generic-param.rs:17:5 | ||
| | | ||
| LL | fn no_return_type<T>(t: T) { | ||
| | - `t` has type `T` | ||
| LL | t(1, 2, true); | ||
| | ^------------ | ||
| | | | ||
| | call expression requires function | ||
| | | ||
| help: consider restricting type parameter `T` with trait `Fn` | ||
| | | ||
| LL | fn no_return_type<T: Fn(i32, i32, bool)>(t: T) { | ||
| | ++++++++++++++++++++ | ||
| 
     | 
||
| error[E0618]: expected function, found `T` | ||
| --> $DIR/suggest-call-on-generic-param.rs:22:5 | ||
| | | ||
| LL | fn existing_bound<T: Copy>(t: T) { | ||
| | - `t` has type `T` | ||
| LL | t(false); | ||
| | ^------- | ||
| | | | ||
| | call expression requires function | ||
| | | ||
| help: consider further restricting type parameter `T` with trait `Fn` | ||
| | | ||
| LL | fn existing_bound<T: Copy + Fn(bool)>(t: T) { | ||
| | ++++++++++ | ||
| 
     | 
||
| error: aborting due to 5 previous errors | ||
| 
     | 
||
| For more information about this error, try `rustc --explain E0618`. | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There several function traits like
Fn,FnmutorFnOnce. It's hard to know which one is most suitable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My original use-case was that there is no bound at all. In that case, we really can't guess much, I suppose, so using
Fnas the most related option seemed reasonable to me.