Skip to content

Commit 6375fe6

Browse files
authored
SafeAttribute supports in properties (#1330)
* SafeAttribute supports in properties * Update Nep11Token.cs * Safe setters are not allowed
1 parent d0d8919 commit 6375fe6

5 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/Neo.Compiler.CSharp/ABI/AbiMethod.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@ public AbiMethod(IMethodSymbol symbol)
2828
: base(symbol, symbol.GetDisplayName(true), symbol.Parameters.Select(p => p.ToAbiParameter()).ToArray())
2929
{
3030
Symbol = symbol;
31-
Safe = symbol.GetAttributes().Any(p => p.AttributeClass!.Name == nameof(scfx::Neo.SmartContract.Framework.Attributes.SafeAttribute));
31+
Safe = GetSafeAttribute(symbol) != null;
32+
if (Safe && symbol.MethodKind == MethodKind.PropertySet)
33+
throw new CompilationException(symbol, DiagnosticId.SafeSetter, "Safe setters are not allowed.");
3234
ReturnType = symbol.ReturnType.GetContractParameterType();
3335
}
36+
37+
private static AttributeData? GetSafeAttribute(IMethodSymbol symbol)
38+
{
39+
AttributeData? attribute = symbol.GetAttributes().FirstOrDefault(p => p.AttributeClass!.Name == nameof(scfx::Neo.SmartContract.Framework.Attributes.SafeAttribute));
40+
if (attribute != null) return attribute;
41+
if (symbol.AssociatedSymbol is IPropertySymbol property)
42+
return property.GetAttributes().FirstOrDefault(p => p.AttributeClass!.Name == nameof(scfx::Neo.SmartContract.Framework.Attributes.SafeAttribute));
43+
return null;
44+
}
3445
}
3546
}

src/Neo.Compiler.CSharp/Diagnostic/DiagnosticId.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ static class DiagnosticId
3939
public const string CapturedStaticFieldNotFound = "NC3007";
4040
public const string InvalidType = "NC3008";
4141
public const string InvalidArgument = "NC3009";
42+
public const string SafeSetter = "NC3010";
4243
}
4344
}

src/Neo.SmartContract.Framework/Attributes/SafeAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace Neo.SmartContract.Framework.Attributes
4848
/// }
4949
/// </code>
5050
/// </remarks>
51-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
51+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false)]
5252
public class SafeAttribute : Attribute
5353
{
5454
}

src/Neo.SmartContract.Framework/Nep11Token.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ public abstract class Nep11Token<TokenState> : TokenContract
3232
protected const byte Prefix_Token = 0x03;
3333
protected const byte Prefix_AccountToken = 0x04;
3434

35-
public sealed override byte Decimals
36-
{
37-
[Safe]
38-
get => 0;
39-
}
35+
[Safe]
36+
public sealed override byte Decimals => 0;
4037

4138
[Safe]
4239
public static UInt160 OwnerOf(ByteString tokenId)

tests/Neo.Compiler.CSharp.TestContracts/Contract_NEP17.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace Neo.Compiler.CSharp.TestContracts
1717
[SupportedStandards(NepStandard.Nep17)]
1818
public class Contract_NEP17 : Nep17Token
1919
{
20-
public override byte Decimals { [Safe] get => 8; }
20+
[Safe]
21+
public override byte Decimals => 8;
2122

2223
public override string Symbol { [Safe] get => "TEST"; }
2324
}

0 commit comments

Comments
 (0)