@@ -1768,6 +1768,170 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17681768 }
17691769 }
17701770
1771+ /// When encountering `Ty < Ty, Ty > call()` in function and method call arguments, suggest
1772+ /// using the turbofish and silence redundant resolve errors.
1773+ pub ( in crate :: late) fn detect_missing_turbofish ( & mut self , arguments : & ' ast [ Box < Expr > ] ) {
1774+ if self . use_injections . is_empty ( ) {
1775+ // Avoid doing unnecessary work, we would have had resolve errors by now if this mistake
1776+ // had happened.
1777+ return ;
1778+ }
1779+ let mut iter = arguments. iter ( ) . peekable ( ) ;
1780+ // Look through every call argument expression, when encountering `a < b`, try to find a
1781+ // closing `c > ::d()` that would make the whole thing be syntactically valid as a
1782+ // fully-qualified path `a::<b, c>::d()`.
1783+ while let Some ( arg) = iter. next ( ) {
1784+ // foo(left < right, bar, baz>::qux())
1785+ // ^^^^ ^^^^^
1786+ let ExprKind :: Binary ( lt, left, right) = & arg. kind else { continue } ;
1787+ // foo(left < right, bar, baz>::qux())
1788+ // ^
1789+ let ast:: BinOpKind :: Lt = lt. node else {
1790+ continue ;
1791+ } ;
1792+ // foo(left < right, bar, baz>::qux())
1793+ // ^^^^ is a path
1794+ let ExprKind :: Path ( _, lt_left) = & left. kind else { continue } ;
1795+ // foo(a::B < right, bar, baz>::qux())
1796+ // ^ ^ lt_left_segments
1797+ let lt_left_segments: Vec < _ > = lt_left. segments . iter ( ) . map ( |seg| seg. into ( ) ) . collect ( ) ;
1798+ // `a::B` is a type-like.
1799+ let lt_res = if let PathResult :: NonModule ( res) =
1800+ self . resolve_path ( & lt_left_segments, Some ( TypeNS ) , None , PathSource :: Expr ( None ) )
1801+ && let Some ( lt_res) = res. full_res ( )
1802+ {
1803+ lt_res
1804+ } else {
1805+ continue ;
1806+ } ;
1807+
1808+ // Same thing we do for `left`, for `right`:
1809+ // foo(left < right, bar, baz>::qux())
1810+ // ^^^^^ is a path
1811+ let ExprKind :: Path ( _, lt_right) = & right. kind else { continue } ;
1812+ let lt_right_segments: Vec < _ > =
1813+ lt_right. segments . iter ( ) . map ( |seg| seg. into ( ) ) . collect ( ) ;
1814+ if let PathResult :: NonModule ( res) =
1815+ self . resolve_path ( & lt_right_segments, Some ( TypeNS ) , None , PathSource :: Expr ( None ) )
1816+ && let Some ( _res) = res. full_res ( )
1817+ {
1818+ } else {
1819+ continue ;
1820+ }
1821+
1822+ // foo(left < right, bar, baz>::qux())
1823+ // next is ^^^ or ^^^^^^^^^^^
1824+ let Some ( mut next) = iter. next ( ) else {
1825+ break ;
1826+ } ;
1827+ let mut other_path_spans = vec ! [ ] ;
1828+ loop {
1829+ match & next. kind {
1830+ // foo(left < right, bar, baz>::qux())
1831+ // ^^^^^^^^^
1832+ ExprKind :: Binary ( gt, gt_left, gt_right)
1833+ // foo(left < right, bar, baz>::qux())
1834+ // ^
1835+ if let ast:: BinOpKind :: Gt = gt. node
1836+ // foo(left < right, bar, baz>::qux())
1837+ // ^^^
1838+ && let ExprKind :: Path ( _, gt_left) = & gt_left. kind
1839+ // foo(left < right, bar, baz>::qux())
1840+ // ^^^^^^^
1841+ && let ExprKind :: Call ( call_expr, _) = & gt_right. kind
1842+ && let ExprKind :: Path ( _, call_path) = & call_expr. kind
1843+ && let gt_left_segments = gt_left
1844+ . segments
1845+ . iter ( )
1846+ . map ( |seg| seg. into ( ) )
1847+ . collect :: < Vec < _ > > ( )
1848+ && let PathResult :: NonModule ( res) = self . resolve_path (
1849+ & gt_left_segments,
1850+ Some ( TypeNS ) ,
1851+ None ,
1852+ PathSource :: Expr ( None ) ,
1853+ )
1854+ && let Some ( _res) = res. full_res ( ) =>
1855+ {
1856+ // We've encountered an expression where we have `Ty < Ty, Ty > call(...)`.
1857+ // A binop between two types is non-sensical in (current?) Rust, and the
1858+ // presence of two binops separated by a comma of less-than followed by
1859+ // greater-than heavily implies a missing turbofish in an expression.
1860+ let mut any = false ;
1861+ let spans: Vec < Span > = lt_left_segments
1862+ . iter ( )
1863+ . map ( |s| s. ident . span )
1864+ . chain ( lt_right_segments. iter ( ) . map ( |s| s. ident . span ) )
1865+ . chain ( gt_left_segments. iter ( ) . map ( |s| s. ident . span ) )
1866+ . chain ( gt_left_segments. iter ( ) . map ( |s| s. ident . span ) )
1867+ . chain ( call_path. segments . iter ( ) . map ( |seg| seg. span ( ) ) )
1868+ . chain ( other_path_spans. iter ( ) . cloned ( ) )
1869+ . collect ( ) ;
1870+ for injection in self . use_injections . iter_mut ( ) {
1871+ // Silence all already constructed resolution errors that are contained
1872+ // by the likely missing turbofish.
1873+ if injection. path . iter ( ) . any ( |s| spans. contains ( & s. ident . span ) ) {
1874+ any = true ;
1875+ injection. err . downgrade_to_delayed_bug ( ) ;
1876+ }
1877+ }
1878+ if any {
1879+ let sp = MultiSpan :: from ( vec ! [ lt. span, gt. span] ) ;
1880+ let mut err =
1881+ self . r . tcx . dcx ( ) . struct_span_err ( sp, "can't compare two types" ) ;
1882+ err. span_label (
1883+ gt. span ,
1884+ "these are parsed as \" less than\" and \" greater than\" " ,
1885+ ) ;
1886+ let name = if let Some ( def_id) = lt_res. opt_def_id ( )
1887+ && let Some ( name) = self . r . tcx . opt_item_name ( def_id)
1888+ {
1889+ name. to_string ( )
1890+ } else {
1891+ lt_right_segments
1892+ . iter ( )
1893+ . map ( |s| s. ident . name . to_string ( ) )
1894+ . collect :: < Vec < String > > ( )
1895+ . join ( "::" )
1896+ } ;
1897+ err. span_suggestion_verbose (
1898+ lt. span . shrink_to_lo ( ) ,
1899+ format ! (
1900+ "you likely intended to write type `{name}` with type parameters, but \
1901+ type parameters in expression contexts require the use of the \
1902+ \" turbofish\" `::<>`",
1903+ ) ,
1904+ "::" . to_string ( ) ,
1905+ Applicability :: MachineApplicable ,
1906+ ) ;
1907+ err. emit ( ) ;
1908+ }
1909+ }
1910+
1911+ // foo(left < right, bat, bar, baz>::qux())
1912+ // ^^^
1913+ ExprKind :: Path ( _, path) => {
1914+ // foo(left < right, bat, bar, baz>::qux())
1915+ // ^^^ or ^^^^^^^^^^^
1916+ if let Some ( n) = iter. next ( ) {
1917+ next = n;
1918+ other_path_spans. extend ( path. segments . iter ( ) . map ( |seg| seg. ident . span ) ) ;
1919+ // Only case where we don't break from the `loop` advancing the parsed
1920+ // call arguments , we've encountered a path that is likely one of the
1921+ // type parameters for the missing turbofish.
1922+ continue ;
1923+ }
1924+ }
1925+
1926+ _ => { }
1927+ }
1928+ // This doesn't look like a turbofish, look for more missing turbofishes on the
1929+ // following arguments.
1930+ break ;
1931+ }
1932+ }
1933+ }
1934+
17711935 fn suggest_at_operator_in_slice_pat_with_range ( & self , err : & mut Diag < ' _ > , path : & [ Segment ] ) {
17721936 let Some ( pat) = self . diag_metadata . current_pat else { return } ;
17731937 let ( bound, side, range) = match & pat. kind {
0 commit comments