Skip to content

Commit

Permalink
[IR] Add an option to ValDef.apply to always set tpt
Browse files Browse the repository at this point in the history
  • Loading branch information
ggevay committed Jul 13, 2018
1 parent f997446 commit aa0a21f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions emma-language/src/main/scala/org/emmalanguage/ast/Bindings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ trait Bindings { this: AST =>
* Creates a type-checked binding definition.
* @param lhs Must be a binding symbol.
* @param rhs The value of this binding (empty by default), owned by `lhs`.
* @param alwaysSetTpt If false, then we only set tpt if the type of `lhs` is different than
* what would be inferred from the `rhs` later. If true, we always write
* out the type (this is necessary when passing a tree to Squid).
* @return `[val|var] lhs [= rhs]`.
*/
def apply(lhs: u.TermSymbol, rhs: u.Tree = Empty()): u.ValDef = {
def apply(lhs: u.TermSymbol, rhs: u.Tree = Empty(), alwaysSetTpt: Boolean = false): u.ValDef = {
assert(is.defined(lhs), s"$this LHS is not defined")
assert(is.binding(lhs), s"$this LHS $lhs is not a binding")
assert(has.nme(lhs), s"$this LHS $lhs has no name")
Expand All @@ -109,7 +112,10 @@ trait Bindings { this: AST =>
|(lhs: `$lhs`, rhs:\n`${u.showCode(rhs)}`\n)
|""".stripMargin.trim)
(Owner.at(lhs)(rhs),
if (lhs.info =:= rhs.tpe.dealias.widen) TypeQuote.empty
// We don't want ValDefs to write out the type if it can be inferred, because of the
// "inaccessible types made explicit issue".
// See https://github.com/emmalanguage/emma/issues/234#issuecomment-260111399
if (lhs.info =:= rhs.tpe.dealias.widen && !alwaysSetTpt) TypeQuote.empty
else TypeQuote(lhs.info))
} else {
assert(lhs.isParameter, s"$this RHS cannot be empty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ trait Values { this: AST =>
* Creates a type-checked value definition.
* @param lhs Must be a value symbol.
* @param rhs The RHS of this value, owned by `lhs`.
* @param alwaysSetTpt See BindingDef.apply
* @return `val lhs = rhs`.
*/
def apply(lhs: u.TermSymbol, rhs: u.Tree): u.ValDef = {
def apply(lhs: u.TermSymbol, rhs: u.Tree, alwaysSetTpt: Boolean = false): u.ValDef = {
assert(is.defined(lhs), s"$this LHS is not defined")
assert(is.value(lhs), s"$this LHS $lhs is not a value")
assert(is.defined(rhs), s"$this RHS is not defined")
BindingDef(lhs, rhs)
BindingDef(lhs, rhs, alwaysSetTpt)
}

def unapply(bind: u.ValDef): Option[(u.TermSymbol, u.Tree)] = bind match {
Expand Down

0 comments on commit aa0a21f

Please sign in to comment.