Skip to content

Commit

Permalink
Fixed a bug related to duplicate bindings (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushdesai committed Aug 12, 2024
1 parent 25f9588 commit 57d200e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public Exception BinOpTypeMismatch(

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

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

public Exception IllegalChooseSubExprType(PParser.ChooseExprContext context, PLanguageType subExprType)
{
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}");
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}");
}

public Exception IllegalChooseSubExprValue(PParser.ChooseExprContext context, int numChoices)
{
return IssueError(context, $"choose expects a parameter with at most 10000 choices, got {numChoices} choices instead.");
return IssueError(context,
$"choose expects a parameter with at most 10000 choices, got {numChoices} choices instead.");
}

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

public Exception ExitFunctionCannotTakeParameters(ParserRuleContext sourceLocation, int count)
{
return IssueError(sourceLocation, $"Exit functions cannot have input parameters, the provided function expects {count} parameters");
return IssueError(sourceLocation,
$"Exit functions cannot have input parameters, the provided function expects {count} parameters");
}

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

public string SpecObservesSetIncompleteWarning(ParserRuleContext ctx, PEvent ev, Machine machine)
{
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!]";
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!]";
}

public Exception DuplicateBindings(ParserRuleContext ctx, Interface @interface)
{
return IssueError(ctx, $"Interface or machine {@interface.Name} is mentioned or bounded multiple times in the module");
}
}
}
1 change: 1 addition & 0 deletions Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,6 @@ Exception DuplicateStartState(
Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner);

String SpecObservesSetIncompleteWarning(ParserRuleContext loc, PEvent ev, Machine machine);
Exception DuplicateBindings(ParserRuleContext loc, Interface @interface);
}
}
7 changes: 7 additions & 0 deletions Src/PCompiler/CompilerCore/TypeChecker/ModuleExprVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public override IPModuleExpr VisitNamedModule([NotNull] PParser.NamedModuleConte
public override IPModuleExpr VisitPrimitiveModuleExpr([NotNull] PParser.PrimitiveModuleExprContext context)
{
var bindings = context._bindslist.Select(VisitBindExpr).ToList();
var seenInterfaces = new List<string>();
foreach (var i in bindings.Select(x => x.Item1))
{
if (seenInterfaces.Contains(i.Name))
throw handler.DuplicateBindings(context, i);
seenInterfaces.Add(i.Name);
}
return new BindModuleExpr(context, bindings);
}

Expand Down

0 comments on commit 57d200e

Please sign in to comment.