Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle backticked names in inffered-type #3791

Merged
merged 1 commit into from Apr 13, 2022

Conversation

dos65
Copy link
Member

@dos65 dos65 commented Apr 5, 2022

Fixes #3785

@dos65 dos65 force-pushed the inferred_type_fix_backticks branch from 4b89643 to 9a39c5b Compare April 5, 2022 13:52
Copy link
Member

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tree.source.atSpan(Span(start, end, start))
case None =>
val ch = text.charAt(idx)
if ch == realName.head then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's too cautious, but can we use headOption to avoid NoSuchElementException here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to get zero length name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible, we could add a require with a proper message just in case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a check for that


def lookup(
idx: Int,
start: Option[(Int, List[Char])],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note]
A pair of next position and trailing realName

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe forge this note into code?

case class Lookup(position: Int, tralingName: List[Char])

Copy link
Member

@kpodsiad kpodsiad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

private def namePos(text: String, tree: untpd.NameTree)(using
Context
): SourcePosition =
val realName = tree.name.stripModuleClassSuffix.lastPart.toString.toList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide some example how tree.name.stripModuleClassSuffix and tree.name.stripModuleClassSuffix.lastPart differs from each other?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kpodsiad I don't know. I just copied this line from from compiler impl for namePos

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, adding info that this line from from compiler impl for namePos will also be helpful for future us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just removed lastPart call. Tests pass without it.

val end = if withBacktick then idx + 1 else idx
tree.source.atSpan(Span(start, end, start))
case None =>
val ch = text.charAt(idx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we guard idx with, for example, tree.sourcePos.end?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case None if idx < text.length should probably suffice

@dos65
Copy link
Member Author

dos65 commented Apr 6, 2022

@tanishiking

Maybe we can backport this namePos strategy (in Scala3) to Interactive#namePos https://github.com/lampepfl/dotty/blob/73cda0c9ad9a23132dabc8f8a7a47663da3ee3fa/compiler/src/dotty/tools/dotc/interactive/SourceTree.scala#L34-L36

I thought about that. Honestly, I don't like the approach in this pr but there is no other ways to fix it.
On compiler side it should be handled in a more elegant way.

tree.source.atSpan(Span(start, end, start))
case None =>
val ch = text.charAt(idx)
if ch == realName.head then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible, we could add a require with a proper message just in case

val end = if withBacktick then idx + 1 else idx
tree.source.atSpan(Span(start, end, start))
case None =>
val ch = text.charAt(idx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case None if idx < text.length should probably suffice

): SourcePosition =
val realName = tree.name.stripModuleClassSuffix.lastPart.toString.toList

def lookup(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be @tailrec ?

@dos65 dos65 force-pushed the inferred_type_fix_backticks branch from 9a39c5b to f03d20e Compare April 12, 2022 11:14
@dos65 dos65 force-pushed the inferred_type_fix_backticks branch from f03d20e to 8427aeb Compare April 12, 2022 11:16
@dos65 dos65 merged commit e56b3be into scalameta:main Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misplaced type annotation on backticked definitions
4 participants