-
Notifications
You must be signed in to change notification settings - Fork 186
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
Small refactoring of explicitResultTypes #1439
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ final class ExplicitResultTypes( | |
if (config.scalacClasspath.isEmpty) { | ||
LazyValue.now(None) | ||
} else { | ||
LazyValue.fromUnsafe { () => | ||
LazyValue.from { () => | ||
ScalafixGlobal.newCompiler( | ||
config.scalacClasspath, | ||
config.scalacOptions, | ||
|
@@ -92,35 +92,32 @@ final class ExplicitResultTypes( | |
} | ||
} | ||
|
||
override def fix(implicit ctx: SemanticDocument): Patch = { | ||
override def fix(implicit ctx: SemanticDocument): Patch = | ||
try unsafeFix() | ||
catch { | ||
case _: CompilerException => | ||
shutdownCompiler() | ||
global.restart() | ||
try unsafeFix() | ||
catch { | ||
case _: CompilerException if !config.fatalWarnings => | ||
// Ignore compiler crashes unless `fatalWarnings = true`. | ||
Patch.empty | ||
} | ||
case _: CompilerException if !config.fatalWarnings => | ||
Patch.empty | ||
} | ||
} | ||
|
||
def unsafeFix()(implicit ctx: SemanticDocument): Patch = { | ||
lazy val types = TypePrinter(global.value, config) | ||
ctx.tree.collect { | ||
case t @ Defn.Val(mods, Pat.Var(name) :: Nil, None, body) | ||
if isRuleCandidate(t, name, mods, body) => | ||
fixDefinition(t, body, types) | ||
|
||
case t @ Defn.Var(mods, Pat.Var(name) :: Nil, None, Some(body)) | ||
if isRuleCandidate(t, name, mods, body) => | ||
fixDefinition(t, body, types) | ||
|
||
case t @ Defn.Def(mods, name, _, _, None, body) | ||
if isRuleCandidate(t, name, mods, body) => | ||
fixDefinition(t, body, types) | ||
}.asPatch | ||
global.value match { | ||
case Some(value) => | ||
val types = new CompilerTypePrinter(value, config) | ||
ctx.tree.collect { | ||
case t @ Defn.Val(mods, Pat.Var(name) :: Nil, None, body) | ||
if isRuleCandidate(t, name, mods, body) => | ||
fixDefinition(t, body, types) | ||
|
||
case t @ Defn.Var(mods, Pat.Var(name) :: Nil, None, Some(body)) | ||
if isRuleCandidate(t, name, mods, body) => | ||
fixDefinition(t, body, types) | ||
|
||
case t @ Defn.Def(mods, name, _, _, None, body) | ||
if isRuleCandidate(t, name, mods, body) => | ||
fixDefinition(t, body, types) | ||
}.asPatch | ||
case None => Patch.empty | ||
} | ||
Comment on lines
+103
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. old code vs new code:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not familiar with |
||
} | ||
|
||
// Don't explicitly annotate vals when the right-hand body is a single call | ||
|
@@ -195,16 +192,21 @@ final class ExplicitResultTypes( | |
} | ||
} | ||
|
||
def defnType(defn: Defn, replace: Token, space: String, types: TypePrinter)( | ||
implicit ctx: SemanticDocument | ||
def defnType( | ||
defn: Defn, | ||
replace: Token, | ||
space: String, | ||
types: CompilerTypePrinter | ||
)(implicit | ||
ctx: SemanticDocument | ||
): Option[Patch] = | ||
for { | ||
name <- defnName(defn) | ||
defnSymbol <- name.symbol.asNonEmpty | ||
patch <- types.toPatch(name.pos, defnSymbol, replace, defn, space) | ||
} yield patch | ||
|
||
def fixDefinition(defn: Defn, body: Term, types: TypePrinter)(implicit | ||
def fixDefinition(defn: Defn, body: Term, types: CompilerTypePrinter)(implicit | ||
ctx: SemanticDocument | ||
): Patch = { | ||
val lst = ctx.tokenList | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler crashes sometimes because it cannot type a part of the AST. If it crashes once, it will crash again even after a retry.
Actually we do that on the file level, which means if one explicit type makes the compiler crash, we don't add any types to the entire file. Should we continue to do that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that the only case? ba74db3 is not very verbose, I am not sure... In any case, if you reproduced the issue, it would be good to document the cases where the compiler crashes and how it behaves via tests.
Catching the exception at a more granular seems like a good idea indeed. Maybe this wasn't done for performance reason because some files would crash the compiler over and over again?