Skip to content

Commit c2031c2

Browse files
authored
Indexer property should not allowed to have mismatched value type (#16023)
* Indexer property is allowed to have mismatched value type warning * More tests * fix rebase conflict
1 parent a1ecaa1 commit c2031c2

19 files changed

+274
-4
lines changed

src/Compiler/Checking/PostInferenceChecks.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,11 +2414,14 @@ let CheckEntityDefn cenv env (tycon: Entity) =
24142414

24152415
// Check to see if the signatures of the both getter and the setter imply the same property type
24162416

2417-
if pinfo.HasGetter && pinfo.HasSetter && not pinfo.IsIndexer then
2417+
if pinfo.HasGetter && pinfo.HasSetter then
24182418
let ty1 = pinfo.DropSetter().GetPropertyType(cenv.amap, m)
24192419
let ty2 = pinfo.DropGetter().GetPropertyType(cenv.amap, m)
24202420
if not (typeEquivAux EraseNone cenv.amap.g ty1 ty2) then
2421-
errorR(Error(FSComp.SR.chkGetterAndSetterHaveSamePropertyType(pinfo.PropertyName, NicePrint.minimalStringOfType cenv.denv ty1, NicePrint.minimalStringOfType cenv.denv ty2), m))
2421+
if g.langVersion.SupportsFeature(LanguageFeature.WarningIndexedPropertiesGetSetSameType) && pinfo.IsIndexer then
2422+
warning(Error(FSComp.SR.chkIndexedGetterAndSetterHaveSamePropertyType(pinfo.PropertyName, NicePrint.minimalStringOfType cenv.denv ty1, NicePrint.minimalStringOfType cenv.denv ty2), m))
2423+
if not pinfo.IsIndexer then
2424+
errorR(Error(FSComp.SR.chkGetterAndSetterHaveSamePropertyType(pinfo.PropertyName, NicePrint.minimalStringOfType cenv.denv ty1, NicePrint.minimalStringOfType cenv.denv ty2), m))
24222425

24232426
hashOfImmediateProps[nm] <- pinfo :: others
24242427

src/Compiler/FSComp.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,7 @@ featureChkNotTailRecursive,"Raises warnings if a member or function has the 'Tai
15881588
featureWhileBang,"'while!' expression"
15891589
featureExtendedFixedBindings,"extended fixed bindings for byref and GetPinnableReference"
15901590
featurePreferStringGetPinnableReference,"prefer String.GetPinnableReference in fixed bindings"
1591+
featureWarningIndexedPropertiesGetSetSameType,"Indexed properties getter and setter must have the same type"
15911592
3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
15921593
3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
15931594
3355,tcNotAnIndexerNamedIndexingNotYetEnabled,"The value '%s' is not a function and does not support index notation."
@@ -1724,6 +1725,7 @@ featureUnmanagedConstraintCsharpInterop,"Interop between C#'s and F#'s unmanaged
17241725
3578,chkCopyUpdateSyntaxInAnonRecords,"This expression is an anonymous record, use {{|...|}} instead of {{...}}."
17251726
3579,alwaysUseTypedStringInterpolation,"Interpolated string contains untyped identifiers. Adding typed format specifiers is recommended."
17261727
3580,tcUnexpectedFunTypeInUnionCaseField,"Unexpected function type in union case field definition. If you intend the field to be a function, consider wrapping the function signature with parens, e.g. | Case of a -> b into | Case of (a -> b)."
1728+
3581,chkIndexedGetterAndSetterHaveSamePropertyType,"An indexed property's getter and setter must have the same type. Property '%s' has getter of type '%s' but setter of type '%s'."
17271729
3582,tcInfoIfFunctionShadowsUnionCase,"This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parentheses."
17281730
3583,unnecessaryParentheses,"Parentheses can be removed."
17291731
3584,tcDotLambdaAtNotSupportedExpression,"Shorthand lambda syntax is only supported for atomic expressions, such as method, property, field or indexer on the implied '_' argument. For example: 'let f = _.Length'."

src/Compiler/Facilities/LanguageFeatures.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type LanguageFeature =
7979
| WhileBang
8080
| ExtendedFixedBindings
8181
| PreferStringGetPinnableReference
82+
| WarningIndexedPropertiesGetSetSameType
8283

8384
/// LanguageVersion management
8485
type LanguageVersion(versionText) =
@@ -185,6 +186,7 @@ type LanguageVersion(versionText) =
185186
// F# preview
186187
LanguageFeature.FromEndSlicing, previewVersion
187188
LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion
189+
LanguageFeature.WarningIndexedPropertiesGetSetSameType, previewVersion
188190
]
189191

190192
static let defaultLanguageVersion = LanguageVersion("default")
@@ -321,6 +323,7 @@ type LanguageVersion(versionText) =
321323
| LanguageFeature.WhileBang -> FSComp.SR.featureWhileBang ()
322324
| LanguageFeature.ExtendedFixedBindings -> FSComp.SR.featureExtendedFixedBindings ()
323325
| LanguageFeature.PreferStringGetPinnableReference -> FSComp.SR.featurePreferStringGetPinnableReference ()
326+
| LanguageFeature.WarningIndexedPropertiesGetSetSameType -> FSComp.SR.featureWarningIndexedPropertiesGetSetSameType ()
324327

325328
/// Get a version string associated with the given feature.
326329
static member GetFeatureVersionString feature =

src/Compiler/Facilities/LanguageFeatures.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type LanguageFeature =
6969
| WhileBang
7070
| ExtendedFixedBindings
7171
| PreferStringGetPinnableReference
72+
| WarningIndexedPropertiesGetSetSameType
7273

7374
/// LanguageVersion management
7475
type LanguageVersion =

src/Compiler/xlf/FSComp.txt.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.it.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.ja.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)