@@ -2044,26 +2044,34 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20442044 // If a tuple of length one was expected and the found expression has
20452045 // parentheses around it, perhaps the user meant to write `(expr,)` to
20462046 // build a tuple (issue #86100)
2047- ( ty:: Tuple ( _) , _) if expected. tuple_fields ( ) . count ( ) == 1 => {
2048- if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span) {
2049- if code. starts_with ( '(' ) && code. ends_with ( ')' ) {
2050- let before_close = span. hi ( ) - BytePos :: from_u32 ( 1 ) ;
2051-
2052- err. span_suggestion (
2053- span. with_hi ( before_close) . shrink_to_hi ( ) ,
2054- "use a trailing comma to create a tuple with one element" ,
2055- "," . into ( ) ,
2056- Applicability :: MaybeIncorrect ,
2057- ) ;
2058- } else {
2059- err. multipart_suggestion (
2060- "use a trailing comma to create a tuple with one element" ,
2061- vec ! [
2062- ( span. shrink_to_lo( ) , "(" . into( ) ) ,
2063- ( span. shrink_to_hi( ) , ",)" . into( ) ) ,
2064- ] ,
2065- Applicability :: MaybeIncorrect ,
2066- ) ;
2047+ ( ty:: Tuple ( _) , _) => {
2048+ if let [ expected_tup_elem] =
2049+ expected. tuple_fields ( ) . collect :: < Vec < _ > > ( ) [ ..]
2050+ {
2051+ if same_type_modulo_infer ( expected_tup_elem, found) {
2052+ if let Ok ( code) =
2053+ self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span)
2054+ {
2055+ if code. starts_with ( '(' ) && code. ends_with ( ')' ) {
2056+ let before_close = span. hi ( ) - BytePos :: from_u32 ( 1 ) ;
2057+
2058+ err. span_suggestion (
2059+ span. with_hi ( before_close) . shrink_to_hi ( ) ,
2060+ "use a trailing comma to create a tuple with one element" ,
2061+ "," . into ( ) ,
2062+ Applicability :: MaybeIncorrect ,
2063+ ) ;
2064+ } else {
2065+ err. multipart_suggestion (
2066+ "use a trailing comma to create a tuple with one element" ,
2067+ vec ! [
2068+ ( span. shrink_to_lo( ) , "(" . into( ) ) ,
2069+ ( span. shrink_to_hi( ) , ",)" . into( ) ) ,
2070+ ] ,
2071+ Applicability :: MaybeIncorrect ,
2072+ ) ;
2073+ }
2074+ }
20672075 }
20682076 }
20692077 }
0 commit comments