- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.1k
Move unsafe operations from Expr to UnsafeExpr #8041
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
      
      
            nicolasstucki
  merged 2 commits into
  scala:master
from
dotty-staging:move-unsafe-operations-out-of-expr
  
      
      
   
  Jan 23, 2020 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package scala.quoted | ||
| package unsafe | ||
|  | ||
| object UnsafeExpr { | ||
|  | ||
| /** Returns the undelying argument that was in the call before inlining. | ||
| * | ||
| * ``` | ||
| * inline foo(x: Int): Int = baz(x, x) | ||
| * foo(bar()) | ||
| * ``` | ||
| * is inlined as | ||
| * ``` | ||
| * val x = bar() | ||
| * baz(x, x) | ||
| * ``` | ||
| * in this case the undelying argument of `x` will be `bar()`. | ||
| * | ||
| * Warning: Using the undelying argument directly in the expansion of a macro may | ||
| * change the parameter semantics as by-value parameter could be re-evaluated. | ||
| */ | ||
| def underlyingArgument[T](expr: Expr[T])(given qctx: QuoteContext): Expr[T] = { | ||
| import qctx.tasty.{given, _} | ||
| expr.unseal.underlyingArgument.seal.asInstanceOf[Expr[T]] | ||
| } | ||
|  | ||
| // TODO generalize for any function arity (see Expr.betaReduce) | ||
| /** Allows inspection or transformation of the body of the expression of function. | ||
| * This body may have references to the arguments of the function which should be closed | ||
| * over if the expression will be spliced. | ||
| * | ||
| * ``` | ||
| * val f: Expr[T => R] = ... | ||
| * UnsafeExpr.open(f) { (body, close) => | ||
| * val newParam: Expr[T] = ... | ||
| * ... | ||
| * close(body)(newParam) // body or part of the body | ||
| * } | ||
| * ``` | ||
| */ | ||
| def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(given qctx: QuoteContext): X = { | ||
| import qctx.tasty.{given, _} | ||
| val (params, bodyExpr) = paramsAndBody(f) | ||
| content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]]) | ||
| } | ||
|  | ||
| def open[T1, T2, R, X](f: Expr[(T1, T2) => R])(content: (Expr[R], [t] => Expr[t] => (Expr[T1], Expr[T2]) => Expr[t]) => X)(given qctx: QuoteContext)(given DummyImplicit): X = { | ||
| import qctx.tasty.{given, _} | ||
| val (params, bodyExpr) = paramsAndBody(f) | ||
| content(bodyExpr, [t] => (e: Expr[t]) => (v1: Expr[T1], v2: Expr[T2]) => bodyFn[t](e.unseal, params, List(v1.unseal, v2.unseal)).seal.asInstanceOf[Expr[t]]) | ||
| } | ||
|  | ||
| def open[T1, T2, T3, R, X](f: Expr[(T1, T2, T3) => R])(content: (Expr[R], [t] => Expr[t] => (Expr[T1], Expr[T2], Expr[T3]) => Expr[t]) => X)(given qctx: QuoteContext)(given DummyImplicit, DummyImplicit): X = { | ||
| import qctx.tasty.{given, _} | ||
| val (params, bodyExpr) = paramsAndBody(f) | ||
| content(bodyExpr, [t] => (e: Expr[t]) => (v1: Expr[T1], v2: Expr[T2], v3: Expr[T3]) => bodyFn[t](e.unseal, params, List(v1.unseal, v2.unseal, v3.unseal)).seal.asInstanceOf[Expr[t]]) | ||
| } | ||
|  | ||
| private def paramsAndBody[R](given qctx: QuoteContext)(f: Expr[Any]) = { | ||
| import qctx.tasty.{given, _} | ||
| val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand | ||
| (params, body.seal.asInstanceOf[Expr[R]]) | ||
| } | ||
|  | ||
| private def bodyFn[t](given qctx: QuoteContext)(e: qctx.tasty.Term, params: List[qctx.tasty.ValDef], args: List[qctx.tasty.Term]): qctx.tasty.Term = { | ||
| import qctx.tasty.{given, _} | ||
| val map = params.map(_.symbol).zip(args).toMap | ||
| new TreeMap { | ||
| override def transformTerm(tree: Term)(given ctx: Context): Term = | ||
| super.transformTerm(tree) match | ||
| case tree: Ident => map.getOrElse(tree.symbol, tree) | ||
| case tree => tree | ||
| }.transformTerm(e) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.