Ticket #336: chk-args-vs-param.patch
File chk-args-vs-param.patch, 4.6 KB (added by hopscc, 11 years ago) |
---|
-
Source/Expr.cobra
2043 2043 2044 2044 numPropertyInits = _verifyAndBindArgs(expr) 2045 2045 _checkTypes(expr) 2046 _type ?= _hoistType(expr) 2046 _type ?= _hoistType(expr) # if _type not set in _checkTypes 2047 2047 2048 # to-do: try and checkout initializer call 2049 2050 if _hasKeywordArg and numPropertyInits > 0 and not .isForAttribute and not .hasError 2048 #try and checkout initializer call 2049 if .args.count-numPropertyInits>0 and expr inherits IdentifierExpr and expr.definition inherits ClassOrStruct and not .hasError 2050 # we skip checks on 0 arg calls here for a more detailed message later from _paramsCountMsg 2051 _checkInitializer(expr, numPropertyInits) 2052 2053 if _hasKeywordArg and numPropertyInits >0 and not .isForAttribute and not .hasError 2051 2054 _makeHelperMethod 2052 2055 2053 2056 def _verifyAndBindArgs(expr as Expr) as int … … 2110 2113 initCall = initdefn 2111 2114 # to-do: verify args and params match exactly re count, assignability 2112 2115 # maybe adjust inheritance tree to use code from CallExpr... 2113 /# 2116 /# 2114 2117 params = initCall.params 2115 2118 print params 2116 2119 if initCall inherits BoxMember # .no-warnings. … … 2123 2126 2124 2127 _checkVisibility(definition, expr) 2125 2128 #/ 2129 else 2130 throw FallThroughException(initdefn) 2126 2131 return initCall 2127 2132 2128 2133 def _argsToCallArgs as List<of Expr> … … 2192 2197 2193 2198 return type 2194 2199 2200 def _checkInitializer(expr as Expr, numPropertyInits as int) 2201 assert expr inherits IdentifierExpr and expr.definition inherits ClassOrStruct 2202 # skip passthrough in arg 2203 if .args.count == 1 and .args[0].type == .typeProvider.passThroughType 2204 return 2205 initCall = _bestInitializer 2206 if not initCall 2207 return 2208 # skip paramlist vari Type 2209 if initCall.hasParams and initCall.params.count == 1 and initCall.params[0].type inherits VariType 2210 return 2211 # augment to check vari arg type same as vari param type 2212 nDefaultedParams = 0 2213 if initCall.hasParams 2214 for p in initCall.params, if p.isOptional, nDefaultedParams += 1 2215 pCount = initCall.params.count 2216 argCount = .args.count - numPropertyInits 2217 if not (argCount >= pCount - nDefaultedParams and argCount <= pCount) 2218 wherePos ='(declared at [initCall.token.fileName]:[initCall.token.lineNum])' 2219 dfltParams = if(nDefaultedParams, ' (with up to [nDefaultedParams] defaulted)', '') 2220 paramStr = '[pCount] parameter' + if(pCount==1, '', 's') 2221 sugg = 'Best choice has [paramStr][dfltParams] [wherePos].' 2222 args = if(numPropertyInits == 0, .args, _argsToCallArgs) 2223 decl = _paramsVsArgsString(initCall.name, initCall.params, args) 2224 paramStr = '[argCount] parameter' + if(argCount==1, '', 's') 2225 .throwError('No initializer in [expr.type.englishName] "[expr.toCobraSource]" with [paramStr]. [sugg][decl]') 2226 2195 2227 def _makeHelperMethod 2196 2228 """ 2197 2229 Add a private helper method to the current box to support extended initializer. -
Tests/120-classes/400-methods/600-named-params/850-bad-init.cobra
1 # Change this later when addInitializer checks1 # Changed for cobra compiler Initializer checks 2 2 3 3 class BadI 4 4 5 5 def main is shared 6 x = BadI('default', 0, 10) # .error. not contain an initializer that takes6 x = BadI('default', 0, 10) # .error. No initializer in class "BadI" with 3 parameters 7 7 # ... 3 arguments - .NET 8 8 # ... "3" arguments - Mono 9 9 CobraCore.noOp(x) … … 11 11 var x = '' 12 12 var a = 0 13 13 14 cue init(sName as String, age as int) # .warning. previous error14 cue init(sName as String, age as int) # (from csc) . warning. previous error 15 15 base.init 16 16 .x = sName 17 17 .a = age -
Tests/120-classes/328-construct-inheritance-error.cobra
6 6 7 7 class B inherits A 8 8 9 cue init # .warning. previous error9 cue init # (from csc) . warning. previous error 10 10 base.init(0) 11 11 12 12 … … 14 14 15 15 def main 16 16 B() 17 B(5) # .error. argument17 B(5) # .error. no initializer