Skip to content

Commit 8b14c28

Browse files
antonsyndclaude
andcommitted
fix(semantic): add bytes operator inference for concatenation, repetition, and comparison
Add bytes type handling to TypeInferenceService.TryInferBuiltinBinaryOp for bytes+bytes (concat), bytes*int/int*bytes (repetition), and bytes==bytes/bytes\!=bytes (equality). Add bytes case to OperatorValidator.SupportsOperator for __add__, __mul__, __eq__, __ne__, and __contains__. Remove bytes_operators.skip to enable integration test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f05e39e commit 8b14c28

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/Sharpy.Compiler.Tests/Integration/TestFixtures/bytes_operators.skip

Whitespace-only changes.

src/Sharpy.Compiler/Semantic/TypeInferenceService.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,30 @@ BinaryOperator.LessThan or BinaryOperator.LessThanOrEqual or
225225
return SemanticType.Str;
226226
}
227227

228+
// Bytes operations: concatenation, repetition, equality
229+
if (left is UserDefinedType { Name: BuiltinNames.Bytes } leftBytes)
230+
{
231+
if (right is UserDefinedType { Name: BuiltinNames.Bytes })
232+
{
233+
var result = op switch
234+
{
235+
BinaryOperator.Add => leftBytes,
236+
BinaryOperator.Equal or BinaryOperator.NotEqual => SemanticType.Bool,
237+
_ => (SemanticType?)null
238+
};
239+
if (result != null) return result;
240+
}
241+
if (op == BinaryOperator.Multiply && TypeUtils.IsInteger(right))
242+
return leftBytes;
243+
}
244+
245+
// Bytes repetition: int * bytes
246+
if (op == BinaryOperator.Multiply && TypeUtils.IsInteger(left) &&
247+
right is UserDefinedType { Name: BuiltinNames.Bytes })
248+
{
249+
return right;
250+
}
251+
228252
// List concatenation
229253
if (left is GenericType { Name: BuiltinNames.List } leftList &&
230254
right is GenericType { Name: BuiltinNames.List } rightList)

src/Sharpy.Compiler/Semantic/Validation/OperatorValidator.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Sharpy.Compiler.Diagnostics;
22
using Sharpy.Compiler.Parser.Ast;
33
using Sharpy.Compiler.Logging;
4+
using Sharpy.Compiler.Shared;
45

56
namespace Sharpy.Compiler.Semantic.Validation;
67

@@ -239,6 +240,12 @@ private bool SupportsOperator(SemanticType type, string dunderName)
239240
return dunderName is DunderNames.Add or DunderNames.Mul or DunderNames.Eq or DunderNames.Ne or DunderNames.Lt or DunderNames.Le or DunderNames.Gt or DunderNames.Ge;
240241
}
241242

243+
// Bytes supports concatenation, repetition, equality, and containment
244+
if (type is UserDefinedType { Name: BuiltinNames.Bytes })
245+
{
246+
return dunderName is DunderNames.Add or DunderNames.Mul or DunderNames.Eq or DunderNames.Ne or DunderNames.Contains;
247+
}
248+
242249
// Builtin numeric types support arithmetic operations
243250
if (type is BuiltinType)
244251
{

0 commit comments

Comments
 (0)