Skip to content

Commit 63285bf

Browse files
committed
Better error message when abstract member impl is wrong - fixes #1203
1 parent 047d472 commit 63285bf

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/fsharp/FSComp.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ derefInsteadOfNot,"The '!' operator is used to dereference a ref cell. Consider
2222
buildUnexpectedTypeArgs,"The non-generic type '%s' does not expect any type arguments, but here is given %d type argument(s)"
2323
returnUsedInsteadOfReturnBang,"Consider using 'return!' instead of 'return'."
2424
yieldUsedInsteadOfYieldBang,"Consider using 'yield!' instead of 'yield'."
25+
tupleRequiredInAbstractMethod,"\nA tuple type is required for one or more arguments. Consider wrapping the given arguments in additional parentheses or review the definition of the interface."
2526
203,buildInvalidWarningNumber,"Invalid warning number '%s'"
2627
204,buildInvalidVersionString,"Invalid version string '%s'"
2728
205,buildInvalidVersionFile,"Invalid version file '%s'"
@@ -592,8 +593,8 @@ tcExpressionWithIfRequiresParenthesis,"This list or array expression includes an
592593
766,tcObjectExpressionsCanOnlyOverrideAbstractOrVirtual,"Only overrides of abstract and virtual members may be specified in object expressions"
593594
767,tcNoAbstractOrVirtualMemberFound,"The member '%s' does not correspond to any abstract or virtual method available to override or implement.%s"
594595
767,tcMemberFoundIsNotAbstractOrVirtual,"The type %s contains the member '%s' but it is not a virtual or abstract method that is available to override or implement.%s"
595-
768,tcArgumentArityMismatch,"The member '%s' does not accept the correct number of arguments, %d arguments are expected"
596-
769,tcArgumentArityMismatchOneOverload,"The member '%s' does not accept the correct number of arguments. One overload accepts %d arguments."
596+
768,tcArgumentArityMismatch,"The member '%s' does not accept the correct number of arguments. %d argument(s) are expected, but %d were given. The required signature is '%s'.%s"
597+
769,tcArgumentArityMismatchOneOverload,"The member '%s' does not accept the correct number of arguments. One overload accepts %d arguments, but %d were given. The required signature is '%s'.%s"
597598
770,tcSimpleMethodNameRequired,"A simple method name is required here"
598599
771,tcPredefinedTypeCannotBeUsedAsSuperType,"The types System.ValueType, System.Enum, System.Delegate, System.MulticastDelegate and System.Array cannot be used as super types in an object expression or class"
599600
772,tcNewMustBeUsedWithNamedType,"'new' must be used with a named type"

src/fsharp/TypeChecker.fs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6210,11 +6210,23 @@ and GetNameAndArityOfObjExprBinding _cenv _env b =
62106210
lookPat pat
62116211

62126212

6213-
and FreshenObjExprAbstractSlot cenv (_env: TcEnv) (implty:TType) virtNameAndArityPairs (bind,bindAttribs,bindName,absSlots:(_ * MethInfo) list) =
6214-
let (NormalizedBinding (_,_,_,_,_,_,synTyparDecls,_,_,_,mBinding,_)) = bind
6213+
and FreshenObjExprAbstractSlot cenv (env: TcEnv) (implty:TType) virtNameAndArityPairs (bind,bindAttribs,bindName,absSlots:(_ * MethInfo) list) =
6214+
let (NormalizedBinding (_,_,_,_,_,_,synTyparDecls,_,_,_,mBinding,_)) = bind
62156215
match absSlots with
62166216
| [] when not (CompileAsEvent cenv.g bindAttribs) ->
62176217
let absSlotsByName = List.filter (fst >> fst >> (=) bindName) virtNameAndArityPairs
6218+
let getSignature absSlot = (NicePrint.stringOfMethInfo cenv.amap mBinding env.DisplayEnv absSlot).Replace("abstract ","")
6219+
let getDetails (absSlot:MethInfo) =
6220+
if absSlot.GetParamTypes(cenv.amap,mBinding,[]) |> List.existsSquared (isTupleTy cenv.g) then
6221+
FSComp.SR.tupleRequiredInAbstractMethod()
6222+
else ""
6223+
6224+
// Compute the argument counts of the member arguments
6225+
let _,synValInfo = GetNameAndArityOfObjExprBinding cenv env bind
6226+
let arity =
6227+
match SynInfo.AritiesOfArgs synValInfo with
6228+
| _::x::_ -> x
6229+
| _ -> 0
62186230

62196231
match absSlotsByName with
62206232
| [] ->
@@ -6232,8 +6244,10 @@ and FreshenObjExprAbstractSlot cenv (_env: TcEnv) (implty:TType) virtNameAndArit
62326244
errorR(Error(FSComp.SR.tcMemberFoundIsNotAbstractOrVirtual(tcref.DisplayName, bindName, ErrorResolutionHints.FormatPredictions predictions),mBinding))
62336245
else
62346246
errorR(Error(FSComp.SR.tcNoAbstractOrVirtualMemberFound(bindName, ErrorResolutionHints.FormatPredictions predictions),mBinding))
6235-
| [(_,absSlot:MethInfo)] -> errorR(Error(FSComp.SR.tcArgumentArityMismatch(bindName, (List.sum absSlot.NumArgs)),mBinding))
6236-
| (_,absSlot:MethInfo) :: _ -> errorR(Error(FSComp.SR.tcArgumentArityMismatchOneOverload(bindName, (List.sum absSlot.NumArgs)),mBinding))
6247+
| [(_,absSlot:MethInfo)] ->
6248+
errorR(Error(FSComp.SR.tcArgumentArityMismatch(bindName, List.sum absSlot.NumArgs, arity, getSignature absSlot, getDetails absSlot),mBinding))
6249+
| (_,absSlot:MethInfo) :: _ ->
6250+
errorR(Error(FSComp.SR.tcArgumentArityMismatchOneOverload(bindName, List.sum absSlot.NumArgs, arity, getSignature absSlot, getDetails absSlot),mBinding))
62376251

62386252
None
62396253

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// #Warnings
2+
//<Expects status="Error" span="(10,16)" id="FS0768">The member 'Function' does not accept the correct number of arguments.</Expects>
3+
//<Expects>A tuple type is required for one or more arguments</Expects>
4+
5+
type IInterface =
6+
abstract Function : (int32 * int32) -> unit
7+
8+
let x =
9+
{ new IInterface with
10+
member this.Function (i, j) = ()
11+
}
12+
13+
exit 0

tests/fsharpqa/Source/Warnings/env.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
SOURCE=WarnIfMissingElseBranch.fs # WarnIfMissingElseBranch.fs
33
SOURCE=ReturnInsteadOfReturnBang.fs # ReturnInsteadOfReturnBang.fs
44
SOURCE=YieldInsteadOfYieldBang.fs # YieldInsteadOfYieldBang.fs
5+
SOURCE=TupleInAbstractMethod.fs # TupleInAbstractMethod.fs
56
SOURCE=InvalidRecord.fs # InvalidRecord.fs
67
SOURCE=CommaInRecCtor.fs # CommaInRecCtor.fs
78
SOURCE=ValidCommaInRecCtor.fs # ValidCommaInRecCtor.fs

0 commit comments

Comments
 (0)