@@ -3771,8 +3771,36 @@ void bindFuncDeclToOperator(TypeChecker &TC, FuncDecl *FD) {
3771
3771
}
3772
3772
3773
3773
if (!op) {
3774
- // FIXME: Add Fix-It introducing an operator declaration?
3775
- TC.diagnose (FD, diag::declared_operator_without_operator_decl);
3774
+ SourceLoc insertionLoc;
3775
+ if (isa<SourceFile>(FD->getParent ())) {
3776
+ // Parent context is SourceFile, insertion location is start of func declaration
3777
+ // or unary operator
3778
+ insertionLoc = FD->isUnaryOperator () ? FD->getAttrs ().getStartLoc () : FD->getStartLoc ();
3779
+ } else {
3780
+ // Finding top-level decl context before SourceFile and inserting before it
3781
+ for (DeclContext *CurContext = FD->getLocalContext ();
3782
+ !isa<SourceFile>(CurContext);
3783
+ CurContext = CurContext->getParent ()) {
3784
+ insertionLoc = CurContext->getAsDecl ()->getStartLoc ();
3785
+ }
3786
+ }
3787
+
3788
+ SmallString<128 > insertion;
3789
+ auto numOfParams = FD->getParameters ()->size ();
3790
+ if (numOfParams == 1 ) {
3791
+ if (FD->getAttrs ().hasAttribute <PrefixAttr>())
3792
+ insertion += " prefix operator " ;
3793
+ else
3794
+ insertion += " postfix operator " ;
3795
+ } else if (numOfParams == 2 ) {
3796
+ insertion += " infix operator " ;
3797
+ }
3798
+
3799
+ insertion += operatorName.str ();
3800
+ insertion += " : <# Precedence Group #>\n " ;
3801
+ InFlightDiagnostic opDiagnostic = TC.diagnose (FD, diag::declared_operator_without_operator_decl);
3802
+ if (insertionLoc.isValid ())
3803
+ opDiagnostic.fixItInsert (insertionLoc, insertion);
3776
3804
return ;
3777
3805
}
3778
3806
0 commit comments