Skip to content

Make sure language imports are detectable syntactically #11193

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

Merged
merged 1 commit into from
Feb 2, 2021

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jan 22, 2021

Make sure that something is a language import if and only if it
looks like one. i.e. is of one of the forms

import language.xyz
import scala.language.xyz
import _root_.scala.language.xyz

This is a pre-requisite for using language imports also during parsing, where we cannot resolve imports.

Using language imports during parsing can make the parser more robust by never considering possibilities that are not enabled bu a language import. For instance, we do not need to get confused with postfix operators.

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM

case Some(bv) => bv
case None =>
var c = ctx.outer
while c.importInfo eq ctx.importInfo do c = c.outer
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: refactor the logic above into a method?

class Context(...):
    /** Returns an outer context that introduces an import clause
     *
     *  For a returned context `ctx`, `ctx.isImportContext == true`.
     */
    def outerImportContext: Option[Context] = ???

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As long as it's used only in one place I think it's better to leave it local

case None =>
var c = ctx.outer
while c.importInfo eq ctx.importInfo do c = c.outer
(c.importInfo != null) && c.importInfo.featureImported(feature)(using c)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible that c == null here, when we get to the very 1st context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because we will hit NoContext first

Copy link
Contributor

Choose a reason for hiding this comment

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

But we have NoContext.outer == null. So the invariant here seems to be that we never hit NoContext.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, better make that explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, NoContext has a null importInfo, so that will stop the recursion.

Make sure that something is a language import if and only if it
looks like one. i.e. is of one of the forms

    import language.xyz
    import scala.language.xyz
    import _root_.scala.language.xyz
@odersky odersky force-pushed the change-language-import branch from 67dcc08 to 26a39ca Compare February 1, 2021 19:26
@odersky odersky merged commit ef315c5 into scala:master Feb 2, 2021
@odersky odersky deleted the change-language-import branch February 2, 2021 08:11
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.

2 participants