Skip to content

Commit

Permalink
Fix another core hacking annoyance
Browse files Browse the repository at this point in the history
Arity checking is simply not working for `nqp::` calls at all.

We could make this check more intelligent, but it would also be slower.
  • Loading branch information
ab5tract committed Sep 22, 2024
1 parent e971e87 commit 87e328d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.raku.comma.psi.type.RakuType;
import org.raku.comma.psi.type.RakuUntyped;
import org.jetbrains.annotations.NotNull;
import org.raku.comma.utils.CommaProjectUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,20 +21,25 @@
public class CallArityIssuesAnnotator implements Annotator {
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (!(element instanceof RakuCodeBlockCall))
return;
if (!(element instanceof RakuCodeBlockCall)) return;
if (CommaProjectUtil.isRakudoCoreProject(element)) return;

PsiElement refElement = element instanceof RakuSubCall
? element.getFirstChild()
: element;
if (CommaProjectUtil.isRakudoCoreProject(refElement)) return;

PsiElement refElement = element instanceof RakuSubCall ? element.getFirstChild() : element;
PsiElement[] args = ((RakuCodeBlockCall) element).getCallArguments();

if (element instanceof RakuMethodCall) {
PsiElement wholeNode = ((RakuMethodCall)element).getWholeCallNode();
PsiElement wholeNode = ((RakuMethodCall) element).getWholeCallNode();
if (wholeNode instanceof RakuPostfixApplication) {
PsiElement operand = ((RakuPostfixApplication)wholeNode).getOperand();
PsiElement operand = ((RakuPostfixApplication) wholeNode).getOperand();
if (operand instanceof RakuPsiElement) {
@NotNull RakuType type = ((RakuPsiElement)operand).inferType();
if (type instanceof RakuUntyped)
@NotNull RakuType type = ((RakuPsiElement) operand).inferType();
if (type instanceof RakuUntyped) {
return;
}
}
}
}
Expand All @@ -42,27 +48,31 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
for (PsiElement arg : args) {
if (arg instanceof RakuPrefixApplication) {
PsiElement prefix = ((RakuPrefixApplication) arg).getPrefix();
if (prefix != null && prefix.getText().startsWith("|"))
if (prefix != null && prefix.getText().startsWith("|")) {
return;
}
}
}

PsiReference ref = refElement.getReference();
if (!(ref instanceof PsiPolyVariantReference))
if (!(ref instanceof PsiPolyVariantReference)) {
return;
}
ResolveResult[] defs = ((PsiPolyVariantReference) ref).multiResolve(false);
if (defs.length == 0)
if (defs.length == 0) {
return;
}

List<AnnotationBuilderWrap> annotations = new ArrayList<>();

MULTI_LOOP:
for (ResolveResult def : defs) {
if (!(def.getElement() instanceof RakuRoutineDecl))
return;
RakuSignature signature = ((RakuRoutineDecl)def.getElement()).getSignatureNode();
if (signature == null)
if (!(def.getElement() instanceof RakuRoutineDecl)) return;

RakuSignature signature = ((RakuRoutineDecl) def.getElement()).getSignatureNode();
if (signature == null) {
signature = RakuElementFactory.createRoutineSignature(element.getProject(), new ArrayList<>());
}
RakuSignature.SignatureCompareResult result = signature.acceptsArguments(args, true, element instanceof RakuMethodCall);
if (result.isAccepted()) {
for (int i = 0; i < annotations.size(); i++) {
Expand All @@ -71,14 +81,17 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
return;
} else {
for (int i = 0; i <= args.length; i++) {
if (CommaProjectUtil.isRakudoCoreProject(args[i])) return;

RakuSignature.MatchFailureReason reason = result.getArgumentFailureReason(i);
if (reason == null)
continue;
if (reason == null) continue;
TextRange argToHighlight = i == 0 && args.length == 0
? refElement.getTextRange()
: i < args.length
? args[i].getTextRange()
: new TextRange(args[0].getTextRange().getStartOffset(), args[args.length - 1].getTextRange().getEndOffset());
? refElement.getTextRange()
: i < args.length
? args[i].getTextRange()
: new TextRange(args[0].getTextRange()
.getStartOffset(), args[args.length - 1].getTextRange()
.getEndOffset());
switch (reason) {
case TOO_MANY_ARGS: {
annotations.add(new AnnotationBuilderWrap(signature, argToHighlight, "Too many positional arguments"));
Expand Down Expand Up @@ -111,10 +124,12 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
} else {
// Multi...
String message = String.format("No multi candidates match (%s)",
annotations.stream().map(an -> String.format("%s: %s", an.signature.summary(RakuUntyped.INSTANCE), an.text)).collect(Collectors.joining(", ")));
annotations.stream()
.map(an -> String.format("%s: %s", an.signature.summary(RakuUntyped.INSTANCE), an.text))
.collect(Collectors.joining(", ")));
holder.newAnnotation(HighlightSeverity.ERROR, message)
.range(refElement)
.create();
.range(refElement)
.create();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/raku/comma/sdk/RakuSdkType.java
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public static List<RakuSymbol> loadModuleSymbols(Project project,
}
}

private static List<RakuSymbol> getNQPSymbols(Project project, RakuFile perl6File) {
private static List<RakuSymbol> getNQPSymbols(Project project, RakuFile rakuFile) {
List<String> ops = new ArrayList<>();
File nqpSymbols = RakuUtils.getResourceAsFile("symbols/nqp.ops");
if (nqpSymbols == null) {
Expand All @@ -564,7 +564,7 @@ private static List<RakuSymbol> getNQPSymbols(Project project, RakuFile perl6Fil
}
}

return new RakuExternalNamesParser(project, perl6File, String.join("\n", ops)).parse().result();
return new RakuExternalNamesParser(project, rakuFile, String.join("\n", ops)).parse().result();
}

public static void contributeParentSymbolsFromCore(@NotNull RakuSymbolCollector collector,
Expand Down

0 comments on commit 87e328d

Please sign in to comment.