Named and default arguments tuned up a little bit

This commit is contained in:
Andrey Breslav
2011-09-29 20:28:03 +04:00
parent 10f16627ef
commit 4c27b5caf8
7 changed files with 65 additions and 46 deletions
@@ -42,7 +42,7 @@ public interface Errors {
return e.getClass().getSimpleName() + ": " + e.getMessage();
}
};
UnresolvedReferenceDiagnosticFactory UNRESOLVED_REFERENCE = UnresolvedReferenceDiagnosticFactory.INSTANCE;
UnresolvedReferenceDiagnosticFactory UNRESOLVED_REFERENCE = new UnresolvedReferenceDiagnosticFactory("Unresolved reference");
RedeclarationDiagnosticFactory REDECLARATION = RedeclarationDiagnosticFactory.INSTANCE;
PsiElementOnlyDiagnosticFactory2<PsiElement, JetType, JetType> TYPE_MISMATCH = PsiElementOnlyDiagnosticFactory2.create(ERROR, "Type mismatch: inferred type is {1} but {0} was expected");
ParameterizedDiagnosticFactory1<Collection<JetKeywordToken>> INCOMPATIBLE_MODIFIERS = new ParameterizedDiagnosticFactory1<Collection<JetKeywordToken>>(ERROR, "Incompatible modifiers: ''{0}''") {
@@ -82,7 +82,7 @@ public interface Errors {
SimpleDiagnosticFactory NO_BACKING_FIELD = SimpleDiagnosticFactory.create(ERROR, "This property does not have a backing field");
SimpleDiagnosticFactory MIXING_NAMED_AND_POSITIONED_ARGUMENTS = SimpleDiagnosticFactory.create(ERROR, "Mixing named and positioned arguments in not allowed");
SimpleDiagnosticFactory ARGUMENT_PASSED_TWICE = SimpleDiagnosticFactory.create(ERROR, "An argument is already passed for this parameter");
SimpleDiagnosticFactory NAMED_PARAMETER_NOT_FOUND = SimpleDiagnosticFactory.create(ERROR, "Cannot find a parameter with this name");
UnresolvedReferenceDiagnosticFactory NAMED_PARAMETER_NOT_FOUND = new UnresolvedReferenceDiagnosticFactory("Cannot find a parameter with this name");//SimpleDiagnosticFactory.create(ERROR, "Cannot find a parameter with this name");
SimpleDiagnosticFactory VARARG_OUTSIDE_PARENTHESES = SimpleDiagnosticFactory.create(ERROR, "Passing value as a vararg is only allowed inside a parenthesized argument list");
SimpleDiagnosticFactory MANY_FUNCTION_LITERAL_ARGUMENTS = SimpleDiagnosticFactory.create(ERROR, "Only one function literal is allowed outside a parenthesized argument list");
@@ -10,8 +10,8 @@ import static org.jetbrains.jet.lang.diagnostics.Severity.ERROR;
*/
public class UnresolvedReferenceDiagnostic extends DiagnosticWithPsiElementImpl<JetReferenceExpression> {
public UnresolvedReferenceDiagnostic(JetReferenceExpression referenceExpression) {
super(Errors.UNRESOLVED_REFERENCE, ERROR, "Unresolved reference", referenceExpression);
public UnresolvedReferenceDiagnostic(JetReferenceExpression referenceExpression, String message) {
super(Errors.UNRESOLVED_REFERENCE, ERROR, message, referenceExpression);
}
@NotNull
@@ -7,11 +7,15 @@ import org.jetbrains.jet.lang.psi.JetReferenceExpression;
* @author abreslav
*/
public class UnresolvedReferenceDiagnosticFactory extends AbstractDiagnosticFactory {
public static final UnresolvedReferenceDiagnosticFactory INSTANCE = new UnresolvedReferenceDiagnosticFactory();
// public static final UnresolvedReferenceDiagnosticFactory INSTANCE = new UnresolvedReferenceDiagnosticFactory();
public UnresolvedReferenceDiagnosticFactory() {}
private final String message;
public UnresolvedReferenceDiagnosticFactory(String message) {
this.message = message;
}
public UnresolvedReferenceDiagnostic on(@NotNull JetReferenceExpression reference) {
return new UnresolvedReferenceDiagnostic(reference);
return new UnresolvedReferenceDiagnostic(reference, message);
}
}
@@ -214,19 +214,20 @@ public class CallResolver {
@Override
public void noValueForParameter(@NotNull BindingTrace trace, @NotNull ValueParameterDescriptor valueParameter) {
ASTNode node;
JetValueArgumentList valueArgumentList = call.getValueArgumentList();
if (valueArgumentList != null) {
node = valueArgumentList.getNode();
}
else if (!call.getFunctionLiteralArguments().isEmpty()) {
node = call.getFunctionLiteralArguments().get(0).getNode();
}
else {
node = callNode;
}
trace.report(NO_VALUE_FOR_PARAMETER.on(node, valueParameter));
// ASTNode node;
//
// JetValueArgumentList valueArgumentList = call.getValueArgumentList();
// if (valueArgumentList != null) {
// node = valueArgumentList.getNode();
// }
// else if (!call.getFunctionLiteralArguments().isEmpty()) {
// node = call.getFunctionLiteralArguments().get(0).getNode();
// }
// else {
// node = callNode;
// }
// trace.report(NO_VALUE_FOR_PARAMETER.on(node, valueParameter));
trace.report(NO_VALUE_FOR_PARAMETER.on(reference, valueParameter));
}
@Override
@@ -2,14 +2,10 @@ package org.jetbrains.jet.lang.resolve.calls;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression;
import org.jetbrains.jet.lang.psi.JetLabelQualifiedExpression;
import org.jetbrains.jet.lang.psi.ValueArgument;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.types.CallMaker;
@@ -49,27 +45,25 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET;
ValueArgument valueArgument = valueArguments.get(i);
if (valueArgument.isNamed()) {
someNamed = true;
ASTNode nameNode = valueArgument.getArgumentName().getNode();
if (somePositioned) {
// temporaryTrace.getErrorHandler().genericError(nameNode, "Mixing named and positioned arguments in not allowed");
temporaryTrace.report(MIXING_NAMED_AND_POSITIONED_ARGUMENTS.on(nameNode));
JetReferenceExpression nameReference = valueArgument.getArgumentName().getReferenceExpression();
ValueParameterDescriptor valueParameterDescriptor = parameterByName.get(valueArgument.getArgumentName().getReferenceExpression().getReferencedName());
if (valueParameterDescriptor == null) {
// temporaryTrace.getErrorHandler().genericError(nameNode, "Cannot find a parameter with this name");
temporaryTrace.report(NAMED_PARAMETER_NOT_FOUND.on(nameReference));
error = true;
}
else {
ValueParameterDescriptor valueParameterDescriptor = parameterByName.get(valueArgument.getArgumentName().getReferenceExpression().getReferencedName());
if (!usedParameters.add(valueParameterDescriptor)) {
// temporaryTrace.getErrorHandler().genericError(nameNode, "An argument is already passed for this parameter");
temporaryTrace.report(ARGUMENT_PASSED_TWICE.on(nameNode));
}
if (valueParameterDescriptor == null) {
// temporaryTrace.getErrorHandler().genericError(nameNode, "Cannot find a parameter with this name");
temporaryTrace.report(NAMED_PARAMETER_NOT_FOUND.on(nameNode));
error = true;
}
else {
temporaryTrace.record(REFERENCE_TARGET, valueArgument.getArgumentName().getReferenceExpression(), valueParameterDescriptor);
argumentsToParameters.put(valueArgument, valueParameterDescriptor);
temporaryTrace.report(ARGUMENT_PASSED_TWICE.on(nameReference));
}
temporaryTrace.record(REFERENCE_TARGET, nameReference, valueParameterDescriptor);
argumentsToParameters.put(valueArgument, valueParameterDescriptor);
}
if (somePositioned) {
// temporaryTrace.getErrorHandler().genericError(nameNode, "Mixing named and positioned arguments in not allowed");
temporaryTrace.report(MIXING_NAMED_AND_POSITIONED_ARGUMENTS.on(nameReference));
error = true;
}
}
else {
@@ -8,6 +8,7 @@ import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.MultiRangeReference;
import com.intellij.psi.PsiElement;
@@ -29,9 +30,7 @@ import org.jetbrains.jet.plugin.quickfix.QuickFixes;
import java.util.Collection;
import java.util.Set;
import static org.jetbrains.jet.lang.resolve.BindingContext.AUTOCAST;
import static org.jetbrains.jet.lang.resolve.BindingContext.AUTO_CREATED_IT;
import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET;
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
/**
* @author abreslav
@@ -52,11 +51,12 @@ public class JetPsiChecker implements Annotator {
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
if (element instanceof JetFile) {
JetFile file = (JetFile) element;
Project project = element.getProject();
try {
final BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(file);
if (errorReportingEnabled) {
Collection<Diagnostic> diagnostics = bindingContext.getDiagnostics();
Collection<Diagnostic> diagnostics = Sets.newLinkedHashSet(bindingContext.getDiagnostics());
Set<PsiElement> redeclarations = Sets.newHashSet();
for (Diagnostic diagnostic : diagnostics) {
Annotation annotation = null;
@@ -68,11 +68,11 @@ public class JetPsiChecker implements Annotator {
if (reference instanceof MultiRangeReference) {
MultiRangeReference mrr = (MultiRangeReference) reference;
for (TextRange range : mrr.getRanges()) {
holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), diagnostic.getMessage()).setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
else {
holder.createErrorAnnotation(referenceExpression, "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
holder.createErrorAnnotation(referenceExpression, diagnostic.getMessage()).setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
else if (diagnostic instanceof RedeclarationDiagnostic) {
@@ -0,0 +1,20 @@
fun foo(a : Int = 1, b : String = "abc") {
}
fun test() {
foo()
foo(2)
foo(<!TYPE_MISMATCH!>""<!>)
foo(b = "")
foo(1, "")
foo(a = 2)
foo(1, "", <!TOO_MANY_ARGUMENTS!>""<!>)
bar(z = "")
<!NO_VALUE_FOR_PARAMETER!>bar<!>()
<!NO_VALUE_FOR_PARAMETER!>bar<!>("")
bar(1, 1, "")
bar(1, 1, "")
bar(1, <!MIXING_NAMED_AND_POSITIONED_ARGUMENTS!>z<!> = "")
<!NO_VALUE_FOR_PARAMETER!>bar<!>(1, <!UNRESOLVED_REFERENCE, MIXING_NAMED_AND_POSITIONED_ARGUMENTS!>zz<!> = "", <!MIXING_NAMED_AND_POSITIONED_ARGUMENTS!><!UNRESOLVED_REFERENCE!>zz<!>.foo<!>)
}