Skip to content

Add reflect Symbol.info and ClassInfo #11664

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
dotc.core.Types.decorateTypeApplications(self).appliedTo(targ)
def appliedTo(targs: List[TypeRepr]): TypeRepr =
dotc.core.Types.decorateTypeApplications(self).appliedTo(targs)
def asSeenFrom(pre: TypeRepr, clazz: Symbol): TypeRepr =
self.asSeenFrom(pre, clazz)
Copy link
Member

Choose a reason for hiding this comment

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

We already expose memberInfo which calls asSeenFrom, in what situation would you need the raw asSeenFrom?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

If we expose symbol.info, without asSeenFrom, the info cannot be safely used.

Copy link
Member

Choose a reason for hiding this comment

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

symbol.info is the info of the symbol itself, I don't see what's unsafe about it, and if you want to see the info from some specific prefix you can do prefix.memberType(symbol). Are there situations where you need to use asSeenFrom directly instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

symbol.info is the info of the symbol itself, I don't see what's unsafe about it, if you want to see the info from some specific prefix you can do prefix.memberType(symbol). Are there situations where you need to use asSeenFrom directly instead?

The concern is that users use symbol.info without rebasing, resulting in error-prone macros. I see that we already have TypeEpr.memberType, thus we don't need asSeenFrom. However, it is good to add the following doc to Symbol.info:

Warning: Symbol.info may contain parameterized types, such as class type parameter, types that depend on this, etc. The method call prefix.memberType(symbol) can be used to get the concrete type of the symbol relative to a prefix.

end extension
end TypeReprMethods

Expand Down
13 changes: 13 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,19 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** The current type applied to given type arguments: `this[targ0, ..., targN]` */
def appliedTo(targs: List[TypeRepr]): TypeRepr

/** This type as seen from prefix `pre` and class `clazz`. This means:
* Replace all thistypes of `clazz` or one of its subclasses
* by `pre` and instantiate all parameters by arguments of `pre`.
* Proceed analogously for thistypes referring to outer classes.
*
* Example:
* class D[T] { def m: T }
* class C extends p.D[Int]
* T.asSeenFrom(ThisType(C), D) (where D is owner of m)
* = Int
*/
def asSeenFrom(pre: TypeRepr, clazz: Symbol): TypeRepr

end extension
}

Expand Down