Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: some compiler warnings #1288

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion neo
Submodule neo updated 155 files
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ private void ConvertExpression(SemanticModel model, ExpressionSyntax syntax, Syn

private bool TryConvertConstant(SemanticModel model, ExpressionSyntax syntax, SyntaxNode? syntaxNode)
{
Optional<object?> constant = model.GetConstantValue(syntax);
if (!constant.HasValue)
var constant = model.GetConstantValue(syntax);
var value = constant.Value;
if (value == null)
return false;

var value = constant.Value;
ITypeSymbol? typeSymbol = GetTypeSymbol(syntaxNode, model);

if (typeSymbol != null)
Expand Down
8 changes: 4 additions & 4 deletions src/Neo.Compiler.CSharp/MethodConvert/Helpers/SlotHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ private void ProcessOutArgument(SemanticModel model, IMethodSymbol methodSymbol,
catch
{
// check if the argument is a discard
var argument = arguments[parameter.Ordinal] as ArgumentSyntax;
if (argument.Expression is not IdentifierNameSyntax { Identifier.ValueText: "_" })
throw new CompilationException(arguments[parameter.Ordinal], DiagnosticId.SyntaxNotSupported,
$"In method {Symbol.Name}, unsupported out argument: {arguments[parameter.Ordinal]}");
var argument = arguments[parameter.Ordinal];
if (argument is not ArgumentSyntax syntax || syntax.Expression is not IdentifierNameSyntax { Identifier.ValueText: "_" })
throw new CompilationException(argument, DiagnosticId.SyntaxNotSupported,
$"In method {Symbol.Name}, unsupported out argument: {argument}");
LdArgSlot(parameter);
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/Neo.Compiler.CSharp/MethodConvert/System/SystemCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ private static string GetKeyFromExpression(LambdaExpression expression, params T
return expression.Body switch
{
MethodCallExpression methodCall => GetMethodCallKey(methodCall, argumentTypes),
MemberExpression { Member: PropertyInfo property } => $"{GetShortTypeName(property.DeclaringType)}.{property.Name}.get",
MemberExpression { Member: FieldInfo field } => $"{GetShortTypeName(field.DeclaringType)}.{field.Name}",
MemberExpression { Member: PropertyInfo property } => $"{GetShortTypeName(property.DeclaringType!)}.{property.Name}.get",
MemberExpression { Member: FieldInfo field } => $"{GetShortTypeName(field.DeclaringType!)}.{field.Name}",
UnaryExpression { NodeType: ExpressionType.Convert } unaryExpression => GetUnaryExpressionKey(unaryExpression),
IndexExpression indexExpression => GetIndexExpressionKey(indexExpression),
_ => throw new ArgumentException("Expression must be a method call, property, field access, or special member.", nameof(expression)),
Expand All @@ -104,6 +104,7 @@ private static string GetKeyFromExpression(LambdaExpression expression, params T
private static string GetMethodCallKey(MethodCallExpression methodCall, Type[] argumentTypes)
{
var method = methodCall.Method;
var declaringType = method.DeclaringType!;
// Static method
if (methodCall.Object == null) return GetMethodKey(method, argumentTypes);

Expand All @@ -114,12 +115,12 @@ private static string GetMethodCallKey(MethodCallExpression methodCall, Type[] a
if (method.IsSpecialName && (methodName.StartsWith("get_Char") || methodName.StartsWith("set_Char")))
{
var accessorType = methodName.StartsWith("get_Char") ? "get" : "set";
return $"{GetShortTypeName(method.DeclaringType)}.this[{parameters}].{accessorType}";
return $"{GetShortTypeName(declaringType)}.this[{parameters}].{accessorType}";
}

if (method.IsGenericMethod)
{
var containingType = GetShortTypeName(method.DeclaringType);
var containingType = GetShortTypeName(declaringType);
var genericArguments = $"<{string.Join(", ", method.GetGenericArguments().Select(GetShortTypeName))}>";
return $"{containingType}.{methodName}{genericArguments}({parameters})";
}
Expand All @@ -131,28 +132,29 @@ private static string GetUnaryExpressionKey(UnaryExpression unaryExpression)
{
var operandType = GetShortTypeName(unaryExpression.Operand.Type);
var targetType = GetShortTypeName(unaryExpression.Type);
return unaryExpression.Method.Name == "op_Implicit"
return unaryExpression.Method!.Name == "op_Implicit"
? $"{targetType}.implicit operator {targetType}({operandType})"
: $"{operandType}.explicit operator {targetType}({operandType})";
}

private static string GetIndexExpressionKey(IndexExpression indexExpression)
{
var indexParams = string.Join(", ", indexExpression.Arguments.Select(arg => GetShortTypeName(arg.Type)));
return $"{GetShortTypeName(indexExpression.Object.Type)}.this[{indexParams}].get";
return $"{GetShortTypeName(indexExpression.Object!.Type)}.this[{indexParams}].get";
}

private static string GetMethodKey(MethodInfo method, Type[] argumentTypes)
{
var containingType = GetShortTypeName(method.DeclaringType);
var declaringType = method.DeclaringType!;
var containingType = GetShortTypeName(declaringType);
var parameters = string.Join(", ", argumentTypes.Select(GetShortTypeName));

switch (method.IsSpecialName)
{
case true when method.Name.StartsWith("get_Char") || method.Name.StartsWith("set_Char"):
{
var accessorType = method.Name.StartsWith("get_Char") ? "get" : "set";
return $"{GetShortTypeName(method.DeclaringType)}.this[{parameters}].{accessorType}";
return $"{containingType}.this[{parameters}].{accessorType}";
}
case true when method.Name.StartsWith("op_"):
{
Expand All @@ -179,7 +181,7 @@ private static string GetShortTypeName(Type type)
{
if (type.IsArray)
{
return GetShortTypeName(type.GetElementType()) + "[]";
return GetShortTypeName(type.GetElementType()!) + "[]";
}

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
Expand Down
6 changes: 3 additions & 3 deletions src/Neo.SmartContract.Analyzer/InitialValueAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public class InitialValueCodeFixProvider : CodeFixProvider

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false)!;
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<FieldDeclarationSyntax>().First();
var declaration = root!.FindToken(diagnosticSpan.Start).Parent!.AncestorsAndSelf().OfType<FieldDeclarationSyntax>().First();

context.RegisterCodeFix(
CodeAction.Create(
Expand All @@ -120,7 +120,7 @@ private async Task<Document> ConvertToLiteralInitializationAsync(Document docume
var newInitializer = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(argument));

var newField = fieldDeclaration
.RemoveNodes(fieldDeclaration.AttributeLists, SyntaxRemoveOptions.KeepNoTrivia)
.RemoveNodes(fieldDeclaration.AttributeLists, SyntaxRemoveOptions.KeepNoTrivia)!
.WithDeclaration(fieldDeclaration.Declaration.WithVariables(
SyntaxFactory.SingletonSeparatedList(
fieldDeclaration.Declaration.Variables[0].WithInitializer(newInitializer))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal static void WriteReport(XmlWriter writer, IReadOnlyList<(CoveredContrac
writer.WriteAttributeString("branch-rate", $"{branchRate:N4}");
writer.WriteAttributeString("branches-covered", $"{branchesCovered}");
writer.WriteAttributeString("branches-valid", $"{branchesValid}");
writer.WriteAttributeString("version", typeof(CoberturaFormat).Assembly.GetName().Version.ToString());
writer.WriteAttributeString("version", typeof(CoberturaFormat).Assembly.GetName().Version?.ToString() ?? "Unknown Version");
writer.WriteAttributeString("timestamp", $"{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}");

writer.WriteStartElement("sources");
Expand Down
5 changes: 4 additions & 1 deletion src/Neo.SmartContract.Testing/PersistingBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ public Block Persist(Transaction[] txs, VMState[] states)

for (int x = 0; x < txs.Length; x++)
{
var transactionState = clonedSnapshot.TryGet(new KeyBuilder(_engine.Native.Ledger.Storage.Id, prefix_Transaction).Add(txs[x].Hash));
var key = new KeyBuilder(_engine.Native.Ledger.Storage.Id, prefix_Transaction).Add(txs[x].Hash);
var transactionState = clonedSnapshot.TryGet(key);
if (transactionState is null)
throw new Exception($"Transaction state not found: {txs[x].Hash}");
transactionState.GetInteroperable<TransactionState>().State = states[x];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Neo.SmartContract.Testing/Storage/Rpc/RpcSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Neo.Persistence;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Neo.SmartContract.Testing.Storage.Rpc;

Expand Down Expand Up @@ -56,12 +57,12 @@ public void Put(byte[] key, byte[] value)
IsDirty = true;
}

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[]? keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
return Store.Seek(keyOrPrefix, direction);
}

public bool TryGet(byte[] key, out byte[]? value)
public bool TryGet(byte[] key, [NotNullWhen(true)] out byte[]? value)
{
return Store.TryGet(key, out value);
}
Expand Down
17 changes: 12 additions & 5 deletions src/Neo.SmartContract.Testing/Storage/Rpc/RpcStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -55,18 +56,24 @@ public void Dispose() { }

#region Rpc calls

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] key, SeekDirection direction)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[]? key, SeekDirection direction)
{
// This(IStore.Seek) is different from LevelDbStore, RocksDbStore and MemoryStore.
if (key is null)
throw new ArgumentNullException(nameof(key));

// This(IStore.Seek) is different from LevelDbStore, RocksDbStore and MemoryStore.
// The following logic has this requirement.
if (key.Length < 4)
throw new ArgumentException("Key must be at least 4 bytes(the first 4 bytes are the contract id)", nameof(key));

if (direction is SeekDirection.Backward)
{
// Not implemented in RPC, we will query all the storage from the contract, and do it manually
// it could return wrong results if we want to get data between contracts

var prefix = key.Take(4).ToArray();
ConcurrentDictionary<byte[], byte[]> data = new();

// We ask for 5 bytes because the minimum prefix is one byte

foreach (var entry in Seek(key.Take(key.Length == 4 ? 4 : 5).ToArray(), SeekDirection.Forward))
{
data.TryAdd(entry.Key, entry.Value);
Expand Down Expand Up @@ -142,7 +149,7 @@ public void Dispose() { }
throw new Exception();
}

public bool TryGet(byte[] key, out byte[]? value)
public bool TryGet(byte[] key, [NotNullWhen(true)] out byte[]? value)
{
var skey = new StorageKey(key);
var requestBody = new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Neo.Compiler.CSharp.TestContracts
{
public class Contract_CheckWitness : SmartContract.Framework.SmartContract
{
public static void Main(UInt160 u)
public static void CheckWitnessAnalysis(UInt160 u)
{
Runtime.CheckWitness(u);
ExecutionEngine.Assert(Runtime.CheckWitness(u));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CheckWitnessTests : DebugAndTestBase<Contract_CheckWitness>
[TestMethod]
public void Test_CheckWitness()
{
CheckWitnessAnalyzer.CheckWitnessVulnerability result = CheckWitnessAnalyzer.AnalyzeCheckWitness(NefFile, Manifest, null);
var result = CheckWitnessAnalyzer.AnalyzeCheckWitness(NefFile, Manifest, null);
Assert.AreEqual(result.droppedCheckWitnessResults.Count, 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class Contract_CheckWitness(Neo.SmartContract.Testing.SmartContr
{
#region Compiled data

public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_CheckWitness"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""main"",""parameters"":[{""name"":""u"",""type"":""Hash160""}],""returntype"":""Void"",""offset"":0,""safe"":false}],""events"":[]},""permissions"":[],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}");
public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_CheckWitness"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""checkWitnessAnalysis"",""parameters"":[{""name"":""u"",""type"":""Hash160""}],""returntype"":""Void"",""offset"":0,""safe"":false}],""events"":[]},""permissions"":[],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}");

/// <summary>
/// Optimization: "All"
Expand All @@ -36,8 +36,8 @@ public abstract class Contract_CheckWitness(Neo.SmartContract.Testing.SmartContr
/// ASSERT [1 datoshi]
/// RET [0 datoshi]
/// </remarks>
[DisplayName("main")]
public abstract void Main(UInt160? u);
[DisplayName("checkWitnessAnalysis")]
public abstract void CheckWitnessAnalysis(UInt160? u);

#endregion
}
Loading