@@ -3641,8 +3641,37 @@ void bindFuncDeclToOperator(TypeChecker &TC, FuncDecl *FD) {
3641
3641
}
3642
3642
3643
3643
if (!op) {
3644
- // FIXME: Add Fix-It introducing an operator declaration?
3645
- TC.diagnose (FD, diag::declared_operator_without_operator_decl);
3644
+ SourceLoc insertionLoc;
3645
+ if (dyn_cast<SourceFile>(FD->getParent ())) {
3646
+ // Parent context is SourceFile, insertion location is start of func declaration
3647
+ // or unary operator
3648
+ insertionLoc = FD->isUnaryOperator () ? FD->getAttrs ().getStartLoc () : FD->getStartLoc ();
3649
+ } else {
3650
+ // Finding top-level decl context before SourceFile and inserting before it
3651
+ for (DeclContext *CurContext = FD->getLocalContext ();
3652
+ CurContext;
3653
+ CurContext = CurContext->getParent ()) {
3654
+ insertionLoc = CurContext->getAsDecl ()->getStartLoc ();
3655
+ if (dyn_cast<SourceFile>(CurContext->getParent ()))
3656
+ break ;
3657
+ }
3658
+ }
3659
+
3660
+ SmallString<128 > insertion;
3661
+ auto numOfParams = FD->getParameters ()->size ();
3662
+ if (numOfParams == 1 ) {
3663
+ if (FD->getAttrs ().hasAttribute <PrefixAttr>())
3664
+ insertion += " prefix operator " ;
3665
+ else
3666
+ insertion += " postfix operator " ;
3667
+ } else if (numOfParams == 2 ) {
3668
+ insertion += " infix operator " ;
3669
+ }
3670
+
3671
+ insertion += operatorName.str ();
3672
+ insertion += " : <# Precedence Group #>\n " ;
3673
+ TC.diagnose (FD, diag::declared_operator_without_operator_decl)
3674
+ .fixItInsert (insertionLoc, insertion);
3646
3675
return ;
3647
3676
}
3648
3677
0 commit comments