Skip to content

Commit

Permalink
Better error message when abstract member impl is wrong - fixes dotne…
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jun 2, 2016
1 parent 20edccb commit fbede5c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ commaInsteadOfSemicolonInRecord,"A ';' is used to separate field values in recor
buildUnexpectedTypeArgs,"The non-generic type '%s' does not expect any type arguments, but here is given %d type argument(s)"
returnUsedInsteadOfReturnBang,"Consider using 'return!' instead of 'return'."
yieldUsedInsteadOfYieldBang,"Consider using 'yield!' instead of 'yield'."
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."
203,buildInvalidWarningNumber,"Invalid warning number '%s'"
204,buildInvalidVersionString,"Invalid version string '%s'"
205,buildInvalidVersionFile,"Invalid version file '%s'"
Expand Down Expand Up @@ -589,8 +590,8 @@ tcExpressionWithIfRequiresParenthesis,"This list or array expression includes an
765,tcExtraneousFieldsGivenValues,"Extraneous fields have been given values"
766,tcObjectExpressionsCanOnlyOverrideAbstractOrVirtual,"Only overrides of abstract and virtual members may be specified in object expressions"
767,tcNoAbstractOrVirtualMemberFound,"The member '%s' does not correspond to any abstract or virtual method available to override or implement"
768,tcArgumentArityMismatch,"The member '%s' does not accept the correct number of arguments, %d arguments are expected"
769,tcArgumentArityMismatchOneOverload,"The member '%s' does not accept the correct number of arguments. One overload accepts %d arguments."
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"
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"
770,tcSimpleMethodNameRequired,"A simple method name is required here"
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"
772,tcNewMustBeUsedWithNamedType,"'new' must be used with a named type"
Expand Down
27 changes: 21 additions & 6 deletions src/fsharp/TypeChecker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6036,16 +6036,31 @@ and GetNameAndArityOfObjExprBinding _cenv _env b =
lookPat pat


and FreshenObjExprAbstractSlot cenv (_env: TcEnv) implty virtNameAndArityPairs (bind,bindAttribs,bindName,absSlots:(_ * MethInfo) list) =
let (NormalizedBinding (_,_,_,_,_,_,synTyparDecls,_,_,_,mBinding,_)) = bind
and FreshenObjExprAbstractSlot cenv (env: TcEnv) implty virtNameAndArityPairs (bind,bindAttribs,bindName,absSlots:(_ * MethInfo) list) =
let (NormalizedBinding (_,_,_,_,_,_,synTyparDecls,_,_,_,mBinding,_)) = bind
match absSlots with
| [] when not (CompileAsEvent cenv.g bindAttribs) ->
let absSlotsByName = List.filter (fst >> fst >> (=) bindName) virtNameAndArityPairs

let getSignature absSlot = (NicePrint.stringOfMethInfo cenv.amap mBinding env.DisplayEnv absSlot).Replace("abstract ","")
let getDetails (absSlot:MethInfo) =
if absSlot.GetParamTypes(cenv.amap,mBinding,[]) |> List.exists (fun xs -> xs |> List.exists (isTupleTy cenv.g)) then
FSComp.SR.tupleRequiredInAbstractMethod()
else ""

// Compute the argument counts of the member arguments
let _,synValInfo = GetNameAndArityOfObjExprBinding cenv env bind
let arity =
match SynInfo.AritiesOfArgs synValInfo with
| _::x::_ -> x
| _ -> 0

match absSlotsByName with
| [] -> errorR(Error(FSComp.SR.tcNoAbstractOrVirtualMemberFound(bindName),mBinding))
| [(_,absSlot:MethInfo)] -> errorR(Error(FSComp.SR.tcArgumentArityMismatch(bindName, (List.sum absSlot.NumArgs)),mBinding))
| (_,absSlot:MethInfo) :: _ -> errorR(Error(FSComp.SR.tcArgumentArityMismatchOneOverload(bindName, (List.sum absSlot.NumArgs)),mBinding))
| [] ->
errorR(Error(FSComp.SR.tcNoAbstractOrVirtualMemberFound(bindName),mBinding))
| [(_,absSlot:MethInfo)] ->
errorR(Error(FSComp.SR.tcArgumentArityMismatch(bindName, List.sum absSlot.NumArgs, arity, getSignature absSlot, getDetails absSlot),mBinding))
| (_,absSlot:MethInfo) :: _ ->
errorR(Error(FSComp.SR.tcArgumentArityMismatchOneOverload(bindName, List.sum absSlot.NumArgs, arity, getSignature absSlot, getDetails absSlot),mBinding))

None

Expand Down
13 changes: 13 additions & 0 deletions tests/fsharpqa/Source/Warnings/TupleInAbstractMethod.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// #Warnings
//<Expects status="Error" span="(10,16)" id="FS0768">The member 'Function' does not accept the correct number of arguments.</Expects>
//<Expects>A tuple type is required for one or more arguments</Expects>

type IInterface =
abstract Function : (int32 * int32) -> unit

let x =
{ new IInterface with
member this.Function (i, j) = ()
}

exit 0
1 change: 1 addition & 0 deletions tests/fsharpqa/Source/Warnings/env.lst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
SOURCE=WarnIfMissingElseBranch.fs # WarnIfMissingElseBranch.fs
SOURCE=ReturnInsteadOfReturnBang.fs # ReturnInsteadOfReturnBang.fs
SOURCE=YieldInsteadOfYieldBang.fs # YieldInsteadOfYieldBang.fs
SOURCE=TupleInAbstractMethod.fs # TupleInAbstractMethod.fs
SOURCE=CommaInRecCtor.fs # CommaInRecCtor.fs
SOURCE=ValidCommaInRecCtor.fs # ValidCommaInRecCtor.fs
SOURCE=ElseBranchHasWrongType.fs # ElseBranchHasWrongType.fs
Expand Down

0 comments on commit fbede5c

Please sign in to comment.