Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit110dc70

Browse files
authored
Merge pull request #3165 from forki/indexer
Better error message when we forgot . in dictionary access
2 parents9512ed1 +49faa92 commit110dc70

File tree

17 files changed

+137
-23
lines changed

17 files changed

+137
-23
lines changed

‎src/fsharp/CompileOps.fs‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ let GetRangeOfDiagnostic(err:PhasedDiagnostic) =
205205

206206
| NotAFunction(_,_,mfun,_)->
207207
Some mfun
208+
209+
| NotAFunctionButIndexer(_,_,_,mfun,_)->
210+
Some mfun
208211

209212
| IllegalFileNameChar(_)-> Some rangeCmdArgs
210213

@@ -243,6 +246,7 @@ let GetDiagnosticNumber(err:PhasedDiagnostic) =
243246
(* DO NOT CHANGE THESE NUMBERS*)
244247
| ErrorFromAddingTypeEquation_->1
245248
| FunctionExpected_->2
249+
| NotAFunctionButIndexer_->3217
246250
| NotAFunction_->3
247251
| FieldNotMutable_->5
248252
| Recursion_->6
@@ -453,8 +457,6 @@ let BakedInMemberConstraintNameE() = DeclareResourceString("BakedInMemberConstra
453457
letBadEventTransformationE()= DeclareResourceString("BadEventTransformation","")
454458
letParameterlessStructCtorE()= DeclareResourceString("ParameterlessStructCtor","")
455459
letInterfaceNotRevealedE()= DeclareResourceString("InterfaceNotRevealed","%s")
456-
letNotAFunction1E()= DeclareResourceString("NotAFunction1","")
457-
letNotAFunction2E()= DeclareResourceString("NotAFunction2","")
458460
letTyconBadArgsE()= DeclareResourceString("TyconBadArgs","%s%d%d")
459461
letIndeterminateTypeE()= DeclareResourceString("IndeterminateType","")
460462
letNameClash1E()= DeclareResourceString("NameClash1","%s%s")
@@ -752,12 +754,15 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
752754
os.Append(ParameterlessStructCtorE().Format)|> ignore
753755
| InterfaceNotRevealed(denv,ity,_)->
754756
os.Append(InterfaceNotRevealedE().Format(NicePrint.minimalStringOfType denv ity))|> ignore
757+
| NotAFunctionButIndexer(_,_,name,_,_)->
758+
match namewith
759+
| Some name-> os.Append(FSComp.SR.notAFunctionButMaybeIndexerWithName name)|> ignore
760+
|_-> os.Append(FSComp.SR.notAFunctionButMaybeIndexer())|> ignore
755761
| NotAFunction(_,_,_,marg)->
756-
if marg.StartColumn=0then
757-
os.Append(NotAFunction1E().Format)|> ignore
762+
if marg.StartColumn=0then
763+
os.Append(FSComp.SR.notAFunctionButMaybeDeclaration())|> ignore
758764
else
759-
os.Append(NotAFunction2E().Format)|> ignore
760-
765+
os.Append(FSComp.SR.notAFunction())|> ignore
761766
| TyconBadArgs(_,tcref,d,_)->
762767
letexp= tcref.TyparsNoRange.Length
763768
if exp=0then

‎src/fsharp/FSComp.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ undefinedNameSuggestionsIntro,"Maybe you want one of the following:"
1616
undefinedNameTypeParameter,"The type parameter %s is not defined."
1717
undefinedNamePatternDiscriminator,"The pattern discriminator '%s' is not defined."
1818
replaceWithSuggestion,"Replace with '%s'"
19+
addIndexerDot,"Add . for indexer access."
1920
listElementHasWrongType,"All elements of a list constructor expression must have the same type. This expression was expected to have type '%s', but here has type '%s'."
2021
arrayElementHasWrongType,"All elements of an array constructor expression must have the same type. This expression was expected to have type '%s', but here has type '%s'."
2122
missingElseBranch,"The 'if' expression is missing an 'else' branch. The 'then' branch has type '%s'. Because 'if' is an expression, and not a statement, add an 'else' branch which returns a value of the same type."
@@ -1413,3 +1414,8 @@ keywordDescriptionUntypedQuotation,"Delimits a untyped code quotation."
14131414
3216,itemNotFoundDuringDynamicCodeGen,"%s '%s' not found in assembly '%s'. A possible cause may be a version incompatibility. You may need to explicitly reference the correct version of this assembly to allow all referenced components to use the correct version."
14141415
3216,itemNotFoundInTypeDuringDynamicCodeGen,"%s '%s' not found in type '%s' from assembly '%s'. A possible cause may be a version incompatibility. You may need to explicitly reference the correct version of this assembly to allow all referenced components to use the correct version."
14151416
descriptionWordIs,"is"
1417+
notAFunction,"This value is not a function and cannot be applied."
1418+
notAFunctionButMaybeIndexerWithName,"This value is not a function and cannot be applied. Did you intend to access the indexer via %s.[index] instead?"
1419+
notAFunctionButMaybeIndexer,"This expression is not a function and cannot be applied. Did you intend to access the indexer via expr.[index] instead?"
1420+
3217,notAFunctionButMaybeIndexerErrorCode,""
1421+
notAFunctionButMaybeDeclaration,"This value is not a function and cannot be applied. Did you forget to terminate a declaration?"

‎src/fsharp/FSStrings.resx‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,6 @@
180180
<dataname="InterfaceNotRevealed"xml:space="preserve">
181181
<value>The type implements the interface '{0}' but this is not revealed by the signature. You should list the interface in the signature, as the interface will be discoverable via dynamic type casts and/or reflection.</value>
182182
</data>
183-
<dataname="NotAFunction1"xml:space="preserve">
184-
<value>This value is not a function and cannot be applied. Did you forget to terminate a declaration?</value>
185-
</data>
186-
<dataname="NotAFunction2"xml:space="preserve">
187-
<value>This value is not a function and cannot be applied</value>
188-
</data>
189183
<dataname="TyconBadArgs"xml:space="preserve">
190184
<value>The type '{0}' expects {1} type argument(s) but is given {2}</value>
191185
</data>

‎src/fsharp/InfoReader.fs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ let GetImmediateIntrinsicPropInfosOfType (optFilter,ad) g amap m typ =
155155
letpinfos= pinfos|> List.filter(IsPropInfoAccessible g amap m ad)
156156
pinfos
157157

158+
// Checks whether the given type has an indexer property.
159+
letIsIndexerType g amap typ=
160+
isArray1DTy g typ||
161+
isListTy g typ||
162+
match tryDestAppTy g typwith
163+
| Some tcref->
164+
let_,entityTy= generalizeTyconRef tcref
165+
letprops= GetImmediateIntrinsicPropInfosOfType(None, AccessibleFromSomeFSharpCode) g amap range0 entityTy
166+
props|> List.exists(fun x-> x.PropertyName="Item")
167+
|_->false
168+
158169

159170
/// Sets of methods up the hierarchy, ignoring duplicates by name and sig.
160171
/// Used to collect sets of virtual methods, protected methods, protected

‎src/fsharp/TcGlobals.fs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
935935
member valtcref_System_Collections_IEqualityComparer= findSysTyconRef sysCollections"IEqualityComparer"
936936
member valtcref_System_Collections_Generic_IEqualityComparer= findSysTyconRef sysGenerics"IEqualityComparer`1"
937937
member valtcref_System_Collections_Generic_Dictionary= findSysTyconRef sysGenerics"Dictionary`2"
938+
member valtcref_System_Collections_Generic_IDictionary= findSysTyconRef sysGenerics"IDictionary`2"
938939

939940
member valtcref_System_IComparable= findSysTyconRef sys"IComparable"
940941
member valtcref_System_IStructuralComparable= findSysTyconRef sysCollections"IStructuralComparable"
@@ -943,7 +944,7 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
943944

944945
member valtcref_LanguagePrimitives= mk_MFCore_tcref fslibCcu"LanguagePrimitives"
945946

946-
947+
member valtcref_System_Collections_Generic_List= findSysTyconRef sysGenerics"List`1"
947948
member valtcref_System_Collections_Generic_IList= findSysTyconRef sysGenerics"IList`1"
948949
member valtcref_System_Collections_Generic_IReadOnlyList= findSysTyconRef sysGenerics"IReadOnlyList`1"
949950
member valtcref_System_Collections_Generic_ICollection= findSysTyconRef sysGenerics"ICollection`1"

‎src/fsharp/TypeChecker.fs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ let mkUnitDelayLambda (g: TcGlobals) m e =
7171
exception BakedInMemberConstraintName of string * range
7272
exception FunctionExpected of DisplayEnv * TType * range
7373
exception NotAFunction of DisplayEnv * TType * range * range
74+
exception NotAFunctionButIndexer of DisplayEnv * TType * string option * range * range
7475
exception Recursion of DisplayEnv * Ident * TType * TType * range
7576
exception RecursiveUseCheckedAtRuntime of DisplayEnv * ValRef * range
7677
exception LetRecEvaluatedOutOfOrder of DisplayEnv * ValRef * ValRef * range
@@ -8236,12 +8237,23 @@ and Propagate cenv overallTy env tpenv (expr: ApplicableExpr) exprty delayed =
82368237
propagate delayedList' mExprAndArg resultTy
82378238
| None ->
82388239
let mArg = arg.Range
8239-
match arg with
8240+
match arg with
82408241
| SynExpr.CompExpr _ -> ()
8241-
| _ ->
8242+
| SynExpr.ArrayOrListOfSeqExpr (false,_,_) ->
8243+
// 'delayed' is about to be dropped on the floor, first do rudimentary checking to get name resolutions in its body
8244+
RecordNameAndTypeResolutions_IdeallyWithoutHavingOtherEffects_Delayed cenv env tpenv delayed
8245+
if IsIndexerType cenv.g cenv.amap expr.Type then
8246+
match expr.Expr with
8247+
| Expr.Val (d,_,_) ->
8248+
error (NotAFunctionButIndexer(denv,overallTy,Some d.DisplayName,mExpr,mArg))
8249+
| _ ->
8250+
error (NotAFunctionButIndexer(denv,overallTy,None,mExpr,mArg))
8251+
else
8252+
error (NotAFunction(denv,overallTy,mExpr,mArg))
8253+
| _ ->
82428254
// 'delayed' is about to be dropped on the floor, first do rudimentary checking to get name resolutions in its body
82438255
RecordNameAndTypeResolutions_IdeallyWithoutHavingOtherEffects_Delayed cenv env tpenv delayed
8244-
error (NotAFunction(denv,overallTy,mExpr,mArg))
8256+
error (NotAFunction(denv,overallTy,mExpr,mArg))
82458257

82468258
propagate delayed expr.Range exprty
82478259

‎src/fsharp/TypeChecker.fsi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ val TypeCheckOneSigFile :
6464
exception BakedInMemberConstraintNameofstring*range
6565
exception FunctionExpectedofDisplayEnv*TType*range
6666
exception NotAFunctionofDisplayEnv*TType*range*range
67+
exception NotAFunctionButIndexerofDisplayEnv*TType*stringoption*range*range
6768
exception RecursionofDisplayEnv*Ast.Ident*TType*TType*range
6869
exception RecursiveUseCheckedAtRuntimeofDisplayEnv*ValRef*range
6970
exception LetRecEvaluatedOutOfOrderofDisplayEnv*ValRef*ValRef*range

‎src/fsharp/ast.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ and
536536
| ForEachofforSeqPoint:SequencePointInfoForForLoop*seqExprOnly:SeqExprOnly*isFromSource:bool*pat:SynPat*enumExpr:SynExpr*bodyExpr:SynExpr*range:range
537537

538538
/// F# syntax: [ expr ], [| expr |]
539-
| ArrayOrListOfSeqExprofisList:bool*expr:SynExpr*range:range
539+
| ArrayOrListOfSeqExprofisArray:bool*expr:SynExpr*range:range
540540

541541
/// CompExpr(isArrayOrList, isNotNakedRefCell, expr)
542542
///

‎tests/fsharp/typecheck/sigs/neg03.bsl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ neg03.fs(103,9,103,14): typecheck error FS0025: Incomplete pattern matches on th
106106

107107
neg03.fs(106,15,106,17): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value '[0]' may indicate a casenot covered by the pattern(s).
108108

109-
neg03.fs(148,23,148,26): typecheck error FS0003: This value isnot a functionand cannot be applied
109+
neg03.fs(148,23,148,26): typecheck error FS0003: This value isnot a functionand cannot be applied.
110110

111111
neg03.fs(151,9,151,23): typecheck error FS0002: This function takes too many arguments,or is usedin a context where a function isnot expected
112112

‎tests/fsharp/typecheck/sigs/neg66.vsbsl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ neg66.fsx(43,4,43,5): parse error FS0010: Unexpected symbol ')' in definition. E
33

44
neg66.fsx(43,4,43,5): parse error FS0010: Unexpected symbol ')' in definition. Expected incomplete structured construct at or before this point or other token.
55

6-
neg66.fsx(30,1,38,33): typecheck error FS0003: This value is not a function and cannot be applied
6+
neg66.fsx(30,1,38,33): typecheck error FS0003: This value is not a function and cannot be applied.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp