Skip to content

Commit 9636fba

Browse files
authored
Let TransparentCompiler respect hash directives like #nowarn (dotnet#16667)
1 parent 921ec7c commit 9636fba

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/Compiler/Service/TransparentCompiler.fs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,29 +1198,16 @@ type internal TransparentCompiler
11981198
tcConfig.flatErrors
11991199
)
12001200

1201-
use _ =
1202-
new CompilationGlobalsScope(errHandler.DiagnosticsLogger, BuildPhase.TypeCheck)
1203-
12041201
// Apply nowarns to tcConfig (may generate errors, so ensure diagnosticsLogger is installed)
12051202
let tcConfig =
12061203
ApplyNoWarnsToTcConfig(tcConfig, parsedMainInput, Path.GetDirectoryName mainInputFileName)
12071204

1208-
// update the error handler with the modified tcConfig
1209-
errHandler.DiagnosticOptions <- tcConfig.diagnosticsOptions
1210-
12111205
let diagnosticsLogger = errHandler.DiagnosticsLogger
12121206

1213-
//let capturingDiagnosticsLogger = CapturingDiagnosticsLogger("TypeCheck")
1214-
1215-
//let diagnosticsLogger =
1216-
// GetDiagnosticsLoggerFilteringByScopedPragmas(
1217-
// false,
1218-
// input.ScopedPragmas,
1219-
// tcConfig.diagnosticsOptions,
1220-
// capturingDiagnosticsLogger
1221-
// )
1207+
let diagnosticsLogger =
1208+
GetDiagnosticsLoggerFilteringByScopedPragmas(false, input.ScopedPragmas, tcConfig.diagnosticsOptions, diagnosticsLogger)
12221209

1223-
//use _ = new CompilationGlobalsScope(diagnosticsLogger, BuildPhase.TypeCheck)
1210+
use _ = new CompilationGlobalsScope(diagnosticsLogger, BuildPhase.TypeCheck)
12241211

12251212
//beforeFileChecked.Trigger fileName
12261213

tests/FSharp.Compiler.ComponentTests/ConstraintSolver/ObjInference.fs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ let x = deserialize "" |> f""", 3, 9, 3, 28
3434
[<Theory>]
3535
[<MemberData(nameof(warningCases))>]
3636
let ``Warning is emitted when type Obj is inferred``(code: string, line1: int, col1: int, line2: int, col2: int) =
37+
let code = $"module M\n{code}"
3738
FSharp code
3839
|> withErrorRanges
3940
|> withWarnOn 3559
4041
|> withLangVersion80
41-
|> typecheck
42+
|> compile
4243
|> shouldFail
43-
|> withSingleDiagnostic (Information 3559, Line line1, Col col1, Line line2, Col col2, message)
44+
|> withSingleDiagnostic (Warning 3559, Line (line1 + 1), Col col1, Line (line2 + 1), Col col2, message)
4445

4546
let quotableNoWarningCases =
4647
[
@@ -110,13 +111,13 @@ let f () = x = x |> ignore""" // measure is inferred as 1, but that's not covere
110111
[<Theory>]
111112
[<MemberData(nameof(quotableWarningCases))>]
112113
let ``Warn also inside quotations of acceptable code``(expr: string, line1: int, col1: int, line2: int, col2: int) =
113-
sprintf "<@ %s @> |> ignore" expr
114+
sprintf "module M\n<@ %s @> |> ignore" expr
114115
|> FSharp
115116
|> withWarnOn 3559
116117
|> withLangVersion80
117-
|> typecheck
118+
|> compile
118119
|> shouldFail
119-
|> withSingleDiagnostic (Information 3559, Line line1, Col (col1 + 3), Line line2, Col (col2 + 3), message)
120+
|> withSingleDiagnostic (Warning 3559, Line (line1 + 1), Col (col1 + 3), Line (line2 + 1), Col (col2 + 3), message)
120121

121122
[<Theory>]
122123
[<MemberData(nameof(quotableNoWarningCases))>]

tests/service/ExprTests.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,3 +3605,27 @@ let ``Test ProjectForWitnesses4 GetWitnessPassingInfo`` useTransparentCompiler =
36053605
printfn "actual:\n\n%A" actual
36063606
actual
36073607
|> shouldPairwiseEqual expected
3608+
3609+
module internal ProjectForNoWarnHashDirective =
3610+
3611+
let fileSource1 = """
3612+
module N.M
3613+
#nowarn "40"
3614+
let rec f = new System.EventHandler(fun _ _ -> f.Invoke(null,null))
3615+
"""
3616+
3617+
let createOptions() = createOptionsAux [fileSource1] []
3618+
3619+
[<TestCase(false)>]
3620+
[<TestCase(true)>]
3621+
[<Test>]
3622+
let ``Test NoWarn HashDirective`` useTransparentCompiler =
3623+
let cleanup, options = ProjectForNoWarnHashDirective.createOptions()
3624+
use _holder = cleanup
3625+
let exprChecker = FSharpChecker.Create(keepAssemblyContents=true, useTransparentCompiler=useTransparentCompiler)
3626+
let wholeProjectResults = exprChecker.ParseAndCheckProject(options) |> Async.RunImmediate
3627+
3628+
for e in wholeProjectResults.Diagnostics do
3629+
printfn "ProjectForNoWarnHashDirective error: <<<%s>>>" e.Message
3630+
3631+
wholeProjectResults.Diagnostics.Length |> shouldEqual 0

0 commit comments

Comments
 (0)