Skip to content

Commit

Permalink
https://github.com/manifold-systems/manifold/issues/520
Browse files Browse the repository at this point in the history
- provide a compiler error indicating a method references must be changed to lambdas where the return type is `auto`
#521
- suppress the warning message because it is indeed handled by string templates
  • Loading branch information
rsmckinney committed Nov 7, 2023
1 parent 600337b commit 9c59b68
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package manifold.api.type;

import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.util.JCDiagnostic;
import manifold.internal.javac.TypeProcessor;

/**
Expand Down Expand Up @@ -50,11 +51,14 @@ default InitOrder initOrder( ICompilerComponent compilerComponent )

/**
* Suppresses the compiler warning/error specified by {@code issueKey}.
*
* @param pos
* @param issueKey The compiler warning/error in question. These are the javac coded message keys such as those
* beginning with "compiler.err.".
* @param args
* @return Returns {@code true} if the message should be suppressed.
*/
default boolean isSuppressed( String issueKey )
default boolean isSuppressed( JCDiagnostic.DiagnosticPosition pos, String issueKey, Object[] args )
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class IssueMsg
public static final IssueMsg MSG_AUTO_RETURN_MORE_SPECIFIC_TYPE = new IssueMsg( "Cannot return 'auto', return a more specific type" );
public static final IssueMsg MSG_AUTO_UNABLE_TO_RESOLVE_TYPE = new IssueMsg( "Unable to infer 'auto' type here" );
public static final IssueMsg MSG_AUTO_CANNOT_INFER_FROM_NULL = new IssueMsg( "'auto' cannot infer from just 'null', cast 'null' or replace 'auto' with a type" );
public static final IssueMsg MSG_ANON_RETURN_METHOD_REF_NOT_SUPPORTED = new IssueMsg( "Method reference '{0}' must be invoked as a lambda expression here" );

private final String _msg;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,17 @@ static boolean checkConcatenation( JCTree.JCLiteral tree, CharSequence chars, Ho
return false;
}

default void checkReference( JCTree.JCMemberReference tree )
{
boolean isAutoReturnType =
tree.sym instanceof Symbol.MethodSymbol && isAutoType( ((Symbol.MethodSymbol)tree.sym).getReturnType() );
if( isAutoReturnType )
{
// Method references not supported with tuple/anonymous return type
getLogger().error( tree.pos, "proc.messager", IssueMsg.MSG_ANON_RETURN_METHOD_REF_NOT_SUPPORTED.get( tree.sym.flatName() ) );
}
}

static boolean isSynthetic( Symbol.MethodSymbol m )
{
return (m.flags() & Flags.SYNTHETIC) != 0 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@ public class ManAttr_11 extends Attr implements ManAttr
return _annotatedTypes.isEmpty() ? null : _annotatedTypes.peek();
}

@Override
public void visitReference( JCTree.JCMemberReference tree )
{
super.visitReference( tree );

checkReference( tree );
}

@Override
public void visitIdent( JCTree.JCIdent tree )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,14 @@ public class ManAttr_17 extends Attr implements ManAttr
return _annotatedTypes.isEmpty() ? null : _annotatedTypes.peek();
}

@Override
public void visitReference( JCTree.JCMemberReference tree )
{
super.visitReference( tree );

checkReference( tree );
}

@Override
public void visitIdent( JCTree.JCIdent tree )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,14 @@ public JCTree.JCAnnotatedType peekAnnotatedType()
return _annotatedTypes.isEmpty() ? null : _annotatedTypes.peek();
}

@Override
public void visitReference( JCTree.JCMemberReference tree )
{
super.visitReference( tree );

checkReference( tree );
}

@Override
public void visitIdent( JCTree.JCIdent tree )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import javax.tools.Diagnostic;

public class ManLog_11 extends Log
{
private Map<DiagnosticHandler, LinkedHashMap<JCTree, Stack<Stack<JCDiagnostic>>>> _suspendedIssues;
private LocklessLazyVar<Class<?>> _extensionTransformerClass;
private final Map<DiagnosticHandler, LinkedHashMap<JCTree, Stack<Stack<JCDiagnostic>>>> _suspendedIssues;
private final LocklessLazyVar<Class<?>> _extensionTransformerClass;

public static Log instance( Context ctx )
{
Expand Down Expand Up @@ -150,17 +150,17 @@ public class ManLog_11 extends Log
//## todo: the error message can't be converted to a warning, make up a custom warning
// report( diags.warning( null, source, pos, (String)ReflectUtil.field( errorKey, "code" ).get(), ReflectUtil.field( errorKey, "args" ).get() ) );
}
else if( !isSuppressedError( errorKey.key() ) )
else if( !isSuppressed( pos, errorKey.key(), errorKey.getArgs() ) )
{
super.error( pos, errorKey );
}
}

private boolean isSuppressedError( String key )
private boolean isSuppressed( JCDiagnostic.DiagnosticPosition pos, String key, Object[] args )
{
for( ICompilerComponent cc: JavacPlugin.instance().getTypeProcessor().getCompilerComponents() )
{
if( cc.isSuppressed( key ) )
if( cc.isSuppressed( pos, key, args ) )
{
return true;
}
Expand All @@ -183,6 +183,12 @@ public class ManLog_11 extends Log
return;
}

if( isSuppressed( issue.getDiagnosticPosition(), issue.getCode(), issue.getArgs() ) )
{
// suppressed from a compiler component
return;
}

LinkedHashMap<JCTree, Stack<Stack<JCDiagnostic>>> suspendedIssues =
_suspendedIssues.get( getDiagnosticHandler() );
if( suspendedIssues == null || suspendedIssues.isEmpty() )
Expand All @@ -207,7 +213,6 @@ public class ManLog_11 extends Log
return false;
}

//noinspection ConstantConditions
return (boolean)ReflectUtil.method( _extensionTransformerClass.get(), "isJailbreakReceiver",
JCTree.JCFieldAccess.class ).invokeStatic( pos );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

public class ManLog_8 extends Log
{
private Map<DiagnosticHandler, LinkedHashMap<JCTree, Stack<Stack<JCDiagnostic>>>> _suspendedIssues;
private LocklessLazyVar<Class<?>> _extensionTransformerClass;
private final Map<DiagnosticHandler, LinkedHashMap<JCTree, Stack<Stack<JCDiagnostic>>>> _suspendedIssues;
private final LocklessLazyVar<Class<?>> _extensionTransformerClass;

public static Log instance( Context ctx )
{
Expand Down Expand Up @@ -165,17 +165,25 @@ public void error( JCDiagnostic.DiagnosticPosition pos, String key, Object... ar
//## todo: the error message can't be converted to a warning, make up a custom warning
// report( diags.warning( source, pos, key, args ) );
}
else if( !isSuppressedError( key ) )
else if( !isSuppressed( pos, key, args ) )
{
super.error( pos, key, args );
}
}

private boolean isSuppressedError( String key )
public void warning( JCDiagnostic.DiagnosticPosition pos, String key, Object... args )
{
if( !isSuppressed( pos, key, args ) )
{
super.warning( pos, key, args );
}
}

private boolean isSuppressed( JCDiagnostic.DiagnosticPosition pos, String key, Object[] args )
{
for( ICompilerComponent cc: JavacPlugin.instance().getTypeProcessor().getCompilerComponents() )
{
if( cc.isSuppressed( key ) )
if( cc.isSuppressed( pos, key, args ) )
{
return true;
}
Expand All @@ -198,6 +206,12 @@ public void report( JCDiagnostic issue )
return;
}

if( isSuppressed( issue.getDiagnosticPosition(), issue.getCode(), issue.getArgs() ) )
{
// suppressed from a compiler component
return;
}

LinkedHashMap<JCTree, Stack<Stack<JCDiagnostic>>> suspendedIssues =
_suspendedIssues.get( getDiagnosticHandler() );
if( suspendedIssues == null || suspendedIssues.isEmpty() )
Expand All @@ -222,7 +236,6 @@ boolean isJailbreakSelect( JCTree.JCFieldAccess pos )
return false;
}

//noinspection ConstantConditions
return (boolean)ReflectUtil.method( _extensionTransformerClass.get(), "isJailbreakReceiver",
JCTree.JCFieldAccess.class ).invokeStatic( pos );
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package manifold.exceptions;

import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.util.JCDiagnostic;
import manifold.api.type.ICompilerComponent;
import manifold.internal.javac.JavacPlugin;
import manifold.internal.javac.TypeProcessor;
Expand All @@ -29,7 +30,7 @@ public void init( BasicJavacTask javacTask, TypeProcessor typeProcessor )
}

@Override
public boolean isSuppressed( String issueKey )
public boolean isSuppressed( JCDiagnostic.DiagnosticPosition pos, String issueKey, Object[] args )
{
return JavacPlugin.instance() != null &&
issueKey != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

public class StringLiteralTemplateProcessor extends TreeTranslator implements ICompilerComponent, TaskListener
{
private static final String SIMPLE_EXPR_DISABLED = "manifold.strings.simple.disabled";
public static final String SIMPLE_EXPR_DISABLED = "manifold.strings.simple.disabled";

private TypeProcessor _tp;
private BasicJavacTask _javacTask;
Expand Down Expand Up @@ -394,6 +394,17 @@ private void replaceNames( JCTree.JCExpression expr, int offset )
expr.accept( new NameReplacer( _javacTask, offset ) );
}

@Override
public boolean isSuppressed( JCDiagnostic.DiagnosticPosition pos, String issueKey, Object[] args )
{
if( issueKey.contains( "unmatched.processor.options" ) && args != null && args.length == 1 )
{
// filter the warning for unmatched processor option
return args[0].toString().contains( SIMPLE_EXPR_DISABLED );
}
return ICompilerComponent.super.isSuppressed( pos, issueKey, args );
}

private static class EscapeMatcher implements IntPredicate
{
private final ManDiagnosticHandler _manDiagnosticHandler;
Expand Down

0 comments on commit 9c59b68

Please sign in to comment.