Skip to content

Commit 57d200e

Browse files
authored
Fixed a bug related to duplicate bindings (#756)
1 parent 25f9588 commit 57d200e

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public Exception BinOpTypeMismatch(
146146

147147
public Exception MoreThanOneParameterForHandlers(ParserRuleContext sourceLocation, int count)
148148
{
149-
return IssueError(sourceLocation, $"functions at entry or exit and do or goto transitions cannot take more than 1 parameter, provided function expects {count} parameters");
149+
return IssueError(sourceLocation,
150+
$"functions at entry or exit and do or goto transitions cannot take more than 1 parameter, provided function expects {count} parameters");
150151
}
151152

152153
public Exception ParseFailure(FileInfo file, string message)
@@ -156,12 +157,14 @@ public Exception ParseFailure(FileInfo file, string message)
156157

157158
public Exception IllegalChooseSubExprType(PParser.ChooseExprContext context, PLanguageType subExprType)
158159
{
159-
return IssueError(context, $"choose expects a parameter of type int (max value) or a collection type (seq, set, or map) got a parameter of type {subExprType}");
160+
return IssueError(context,
161+
$"choose expects a parameter of type int (max value) or a collection type (seq, set, or map) got a parameter of type {subExprType}");
160162
}
161163

162164
public Exception IllegalChooseSubExprValue(PParser.ChooseExprContext context, int numChoices)
163165
{
164-
return IssueError(context, $"choose expects a parameter with at most 10000 choices, got {numChoices} choices instead.");
166+
return IssueError(context,
167+
$"choose expects a parameter with at most 10000 choices, got {numChoices} choices instead.");
165168
}
166169

167170
public Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner)
@@ -322,7 +325,8 @@ public Exception BareLoopControlFlow(string stmtName, ParserRuleContext context)
322325

323326
public Exception ExitFunctionCannotTakeParameters(ParserRuleContext sourceLocation, int count)
324327
{
325-
return IssueError(sourceLocation, $"Exit functions cannot have input parameters, the provided function expects {count} parameters");
328+
return IssueError(sourceLocation,
329+
$"Exit functions cannot have input parameters, the provided function expects {count} parameters");
326330
}
327331

328332
private Exception IssueError(ParserRuleContext location, string message)
@@ -342,7 +346,13 @@ private string DeclarationName(IPDecl method)
342346

343347
public string SpecObservesSetIncompleteWarning(ParserRuleContext ctx, PEvent ev, Machine machine)
344348
{
345-
return $"[!Warning!]\n[{locationResolver.GetLocation(ctx, ctx.start)}] Event {ev.Name} is not in the observes list of the spec machine {machine.Name}. The event-handler is never triggered as the event is not observed by the spec.\n[!Warning!]";
349+
return
350+
$"[!Warning!]\n[{locationResolver.GetLocation(ctx, ctx.start)}] Event {ev.Name} is not in the observes list of the spec machine {machine.Name}. The event-handler is never triggered as the event is not observed by the spec.\n[!Warning!]";
351+
}
352+
353+
public Exception DuplicateBindings(ParserRuleContext ctx, Interface @interface)
354+
{
355+
return IssueError(ctx, $"Interface or machine {@interface.Name} is mentioned or bounded multiple times in the module");
346356
}
347357
}
348358
}

Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs

+1
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,6 @@ Exception DuplicateStartState(
120120
Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner);
121121

122122
String SpecObservesSetIncompleteWarning(ParserRuleContext loc, PEvent ev, Machine machine);
123+
Exception DuplicateBindings(ParserRuleContext loc, Interface @interface);
123124
}
124125
}

Src/PCompiler/CompilerCore/TypeChecker/ModuleExprVisitor.cs

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public override IPModuleExpr VisitNamedModule([NotNull] PParser.NamedModuleConte
9898
public override IPModuleExpr VisitPrimitiveModuleExpr([NotNull] PParser.PrimitiveModuleExprContext context)
9999
{
100100
var bindings = context._bindslist.Select(VisitBindExpr).ToList();
101+
var seenInterfaces = new List<string>();
102+
foreach (var i in bindings.Select(x => x.Item1))
103+
{
104+
if (seenInterfaces.Contains(i.Name))
105+
throw handler.DuplicateBindings(context, i);
106+
seenInterfaces.Add(i.Name);
107+
}
101108
return new BindModuleExpr(context, bindings);
102109
}
103110

0 commit comments

Comments
 (0)