bug icon indicating copy to clipboard operation
bug copied to clipboard

Overriding java methods containing varargs gives warning

Open Arthurm1 opened this issue 3 years ago • 6 comments

Reproduction steps

Scala version: 2.13.8

Override java.nio.file.Path as follows...

import java.nio.file.Path
import java.nio.file.FileSystem
import java.net.URI
import java.nio.file.LinkOption
import java.nio.file.{WatchKey, WatchService}
import java.nio.file.WatchEvent.{Kind, Modifier}

class FooPath extends Path {

  override def getFileSystem(): FileSystem = { null }

  override def isAbsolute(): Boolean = { false }

  override def getRoot(): Path = { null }

  override def getFileName(): Path = { null }

  override def getParent(): Path = { null }

  override def getNameCount(): Int ={ 0 }

  override def getName(x$1: Int): Path ={ null }

  override def subpath(x$1: Int, x$2: Int): Path = { null }

  override def startsWith(x$1: Path): Boolean = { false }

  override def endsWith(x$1: Path): Boolean = { false }

  override def normalize(): Path = { null }

  override def resolve(x$1: Path): Path = { null }

  override def relativize(x$1: Path): Path = { null }

  override def toUri(): URI = { null }

  override def toAbsolutePath(): Path = { null }

  override def toRealPath(x$1: LinkOption*): Path = { null }

  override def register(x$1: WatchService, x$2: Array[Kind[_ <: Object]], x$3: Modifier*): WatchKey = { null }

  override def compareTo(x$1: Path): Int = { 0 }
}

Problem

The toRealPath and register methods have warnings that the varargs parameters are not used. Here's a screenshot in Metals... image

The methods that don't contain vararg params correctly have no warnings.

Also - Metals doesn't show codelens for the varargs methods for goto parent. Not sure if this stems from the same place as the warnings.

It's fine when overriding a Scala trait with varargs - only overriding a Java class seems to be an issue.

See https://github.com/scalameta/metals/issues/3939 for original bug report at Metals.

Arthurm1 avatar May 24 '22 14:05 Arthurm1

When investigating the issue in semanticdb I saw that symbol.overridenSymbols doesn't yield the parent method, so this might be related to the way the parameters are compared when looking for parent symbols.

tgodzik avatar May 24 '22 14:05 tgodzik

Note that current -Wunused stops warning on the example because of the "trivial" RHS.

som-snytt avatar May 24 '22 17:05 som-snytt

I will PR an OverridingPairs.Cursor that handles the special case of Scala varargs overriding Java. The bridge is emitted in RefChecks, which RefChecks relies on while RefChecking. Bridges for @varargs are at erasure. A different approach to this issue would be to move the warning to RefChecks.

Also I appreciated the Java varargs pun: as follows...

som-snytt avatar May 25 '22 00:05 som-snytt

I will PR an OverridingPairs.Cursor that handles the special case of Scala varargs overriding Java. The bridge is emitted in RefChecks, which RefChecks relies on while RefChecking. Bridges for @varargs are at erasure. A different approach to this issue would be to move the warning to RefChecks.

Thanks!

Also I appreciated the Java varargs pun: as follows...

I only wish it was intentional

Arthurm1 avatar May 25 '22 09:05 Arthurm1

I embraced the Path example, but learned there are default methods, as noticed by test running on JVM 8.

som-snytt avatar May 25 '22 20:05 som-snytt

How about java.nio.file.Watchable. It's varargs all the way down.

Arthurm1 avatar May 25 '22 21:05 Arthurm1

@som-snytt is your PR still something you might return to? or should I un-assign you and/or put a "help wanted" label?

SethTisue avatar Aug 21 '23 19:08 SethTisue

Neither PR is very satisfying: the first is many LOC for a little issue; the second (linting later) is a bigger change albeit with tradeoffs (such as linting macros). Maybe macros could be linted at expansion. I will resubmit.

som-snytt avatar Aug 22 '23 01:08 som-snytt