Skip to content

Commit 24208f0

Browse files
authored
Fix access checking for properies setters in attribute arguments (#18581)
1 parent 9720080 commit 24208f0

17 files changed

+126
-2
lines changed

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11399,7 +11399,12 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn
1139911399

1140011400
let mkAttribExpr e =
1140111401
AttribExpr(e, EvalLiteralExprOrAttribArg g e)
11402-
11402+
11403+
let checkPropSetterAttribAccess m (pinfo: PropInfo) =
11404+
let setterMeth = pinfo.SetterMethod
11405+
if not <| IsTypeAndMethInfoAccessible cenv.amap m ad ad setterMeth then
11406+
errorR(Error (FSComp.SR.tcPropertyCannotBeSetPrivateSetter(pinfo.PropertyName), m))
11407+
1140311408
let namedAttribArgMap =
1140411409
attributeAssignedNamedItems |> List.map (fun (CallerNamedArg(id, CallerArg(callerArgTy, m, isOpt, callerArgExpr))) ->
1140511410
if isOpt then error(Error(FSComp.SR.tcOptionalArgumentsCannotBeUsedInCustomAttribute(), m))
@@ -11411,6 +11416,7 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn
1141111416
| Item.Property (info = [pinfo]) ->
1141211417
if not pinfo.HasSetter then
1141311418
errorR(Error(FSComp.SR.tcPropertyCannotBeSet0(), m))
11419+
checkPropSetterAttribAccess m pinfo
1141411420
id.idText, true, pinfo.GetPropertyType(cenv.amap, m)
1141511421
| Item.ILField finfo ->
1141611422
CheckILFieldInfoAccessible g cenv.amap m ad finfo

src/Compiler/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,7 @@ notAFunctionButMaybeDeclaration,"This value is not a function and cannot be appl
15261526
3245,tcCopyAndUpdateNeedsRecordType,"The input to a copy-and-update expression that creates an anonymous record must be either an anonymous record or a record"
15271527
3246,tcAugmentationsCannotHaveAttributes,"Attributes cannot be applied to type extensions."
15281528
3247,couldNotLoadDependencyManagerExtension,"The dependency manager extension %s could not be loaded. Message: %s"
1529+
3248,tcPropertyCannotBeSetPrivateSetter,"Property '%s' cannot be set because the setter is private"
15291530
3250,expressionHasNoName,"Expression does not have a name."
15301531
3251,chkNoFirstClassNameOf,"Using the 'nameof' operator as a first-class function value is not permitted."
15311532
3252,tcIllegalByrefsInOpenTypeDeclaration,"Byref types are not allowed in an open type declaration."

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

Lines changed: 5 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: 6 additions & 1 deletion
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: 5 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: 5 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: 5 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: 5 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.ko.xlf

Lines changed: 5 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.pl.xlf

Lines changed: 5 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.pt-BR.xlf

Lines changed: 5 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.ru.xlf

Lines changed: 5 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.tr.xlf

Lines changed: 5 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.zh-Hans.xlf

Lines changed: 5 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.zh-Hant.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace FSharp.Compiler.ComponentTests.Attributes
2+
3+
open Xunit
4+
open FSharp.Test.Compiler
5+
6+
module AttributeCtorSetPropAccess =
7+
let private csLib = """
8+
using System;
9+
10+
namespace TestAttribute
11+
{
12+
[AttributeUsage(AttributeTargets.All, Inherited = false)]
13+
public class FooAttribute : Attribute
14+
{
15+
public int X { get; %s set; }
16+
}
17+
}
18+
"""
19+
20+
let private fsCode = """
21+
namespace Other
22+
open System
23+
open TestAttribute
24+
25+
module TestAttributeModule =
26+
27+
[<FooAttribute(X = 100)>]
28+
let myFunction() = ()
29+
"""
30+
31+
[<Theory>]
32+
[<InlineData("private")>]
33+
[<InlineData("internal")>]
34+
[<InlineData("protected")>]
35+
[<InlineData("private protected")>]
36+
[<InlineData("protected internal")>]
37+
let ``Cannot set property outside its accessibility scope``(modifier: string): unit =
38+
FSharp fsCode
39+
|> withReferences [(CSharp <| csLib.Replace("%s", modifier))]
40+
|> compile
41+
|> shouldFail
42+
|> withDiagnostics [ (ErrorType.Error 3248, Line 8, Col 28, Line 8, Col 31, "Property 'X' cannot be set because the setter is private") ]
43+
|> ignore
44+
45+
[<Fact>]
46+
let ``Can set property inside its accessibility scope``(): unit =
47+
FSharp fsCode
48+
|> withReferences [(CSharp <| csLib.Replace("%s", ""))]
49+
|> compile
50+
|> shouldSucceed
51+
|> ignore

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@
345345
<Compile Include="FSharpChecker\TransparentCompiler.fs" />
346346
<Compile Include="FSharpChecker\SymbolUse.fs" />
347347
<Compile Include="FSharpChecker\FindReferences.fs" />
348+
<Compile Include="Attributes\AttributeCtorSetPropAccess.fs"/>
348349
</ItemGroup>
349350

350351
<ItemGroup>

0 commit comments

Comments
 (0)