@@ -223,13 +223,31 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
223223 if candidates. is_empty ( ) && is_expected ( Res :: Def ( DefKind :: Enum , crate_def_id) ) {
224224 let enum_candidates =
225225 self . r . lookup_import_candidates ( ident, ns, & self . parent_scope , is_enum_variant) ;
226- let mut enum_candidates = enum_candidates
227- . iter ( )
228- . map ( |suggestion| import_candidate_to_enum_paths ( & suggestion) )
229- . collect :: < Vec < _ > > ( ) ;
230- enum_candidates. sort ( ) ;
231226
232227 if !enum_candidates. is_empty ( ) {
228+ if let ( PathSource :: Type , Some ( Expr { kind : ExprKind :: Type ( expr, ty) , .. } ) ) =
229+ ( source, self . diagnostic_metadata . current_expr )
230+ {
231+ if self
232+ . r
233+ . session
234+ . parse_sess
235+ . type_ascription_path_suggestions
236+ . borrow ( )
237+ . contains ( & expr. span . between ( ty. span ) )
238+ {
239+ // Already reported this issue on the lhs of the type ascription.
240+ err. delay_as_bug ( ) ;
241+ return ( err, candidates) ;
242+ }
243+ }
244+
245+ let mut enum_candidates = enum_candidates
246+ . iter ( )
247+ . map ( |suggestion| import_candidate_to_enum_paths ( & suggestion) )
248+ . collect :: < Vec < _ > > ( ) ;
249+ enum_candidates. sort ( ) ;
250+
233251 // Contextualize for E0412 "cannot find type", but don't belabor the point
234252 // (that it's a variant) for E0573 "expected type, found variant".
235253 let preamble = if res. is_none ( ) {
@@ -484,10 +502,21 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
484502 match source {
485503 PathSource :: Expr ( Some (
486504 parent @ Expr { kind : ExprKind :: Field ( ..) | ExprKind :: MethodCall ( ..) , .. } ,
487- ) ) => {
488- path_sep ( err, & parent) ;
489- }
490- PathSource :: Expr ( None ) if followed_by_brace => {
505+ ) ) if path_sep ( err, & parent) => { }
506+ PathSource :: Expr (
507+ None
508+ | Some ( Expr {
509+ kind :
510+ ExprKind :: Path ( ..)
511+ | ExprKind :: Binary ( ..)
512+ | ExprKind :: Unary ( ..)
513+ | ExprKind :: If ( ..)
514+ | ExprKind :: While ( ..)
515+ | ExprKind :: ForLoop ( ..)
516+ | ExprKind :: Match ( ..) ,
517+ ..
518+ } ) ,
519+ ) if followed_by_brace => {
491520 if let Some ( sp) = closing_brace {
492521 err. multipart_suggestion (
493522 "surround the struct literal with parentheses" ,
@@ -508,11 +537,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
508537 ) ;
509538 }
510539 }
511- PathSource :: Expr (
512- None | Some ( Expr { kind : ExprKind :: Call ( ..) | ExprKind :: Path ( ..) , .. } ) ,
513- )
514- | PathSource :: TupleStruct ( _)
515- | PathSource :: Pat => {
540+ PathSource :: Expr ( _) | PathSource :: TupleStruct ( _) | PathSource :: Pat => {
516541 let span = match & source {
517542 PathSource :: Expr ( Some ( Expr {
518543 span, kind : ExprKind :: Call ( _, _) , ..
@@ -609,7 +634,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
609634 ) ;
610635 }
611636 } else {
612- err. note ( "did you mean to use one of the enum's variants? " ) ;
637+ err. note ( "you might have meant to use one of the enum's variants" ) ;
613638 }
614639 }
615640 ( Res :: Def ( DefKind :: Struct , def_id) , _) if ns == ValueNS => {
0 commit comments