Skip to content

Commit 66e1637

Browse files
authored
Scoped nowarn (#18049)
1 parent c4ed80c commit 66e1637

File tree

987 files changed

+3474
-1648
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

987 files changed

+3474
-1648
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.300.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* Add a switch to determine whether to generate a default implementation body for overridden method when completing. [PR #18341](https://github.com/dotnet/fsharp/pull/18341)
4444
* Use a more accurate range for CE Combine methods. [PR #18394](https://github.com/dotnet/fsharp/pull/18394)
4545
* Enable TypeSubsumptionCache for IDE use. [PR #18499](https://github.com/dotnet/fsharp/pull/18499)
46-
46+
* Scoped Nowarn: Add the #warnon compiler directive ([Language suggestion #278](https://github.com/fsharp/fslang-suggestions/issues/278), [RFC FS-1146 PR](https://github.com/fsharp/fslang-design/pull/782), [PR #18049](https://github.com/dotnet/fsharp/pull/18049))
4747

4848
### Changed
4949
* FSharpCheckFileResults.ProjectContext.ProjectOptions will not be available when using the experimental Transparent Compiler feature. ([PR #18205](https://github.com/dotnet/fsharp/pull/18205))

docs/release-notes/.Language/preview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Support ValueOption + Struct attribute as optional parameter for methods ([Language suggestion #1136](https://github.com/fsharp/fslang-suggestions/issues/1136), [PR #18098](https://github.com/dotnet/fsharp/pull/18098))
77
* Allow `_` in `use!` bindings values (lift FS1228 restriction) ([PR #18487](https://github.com/dotnet/fsharp/pull/18487))
88
* Warn when `unit` is passed to an `obj`-typed argument ([PR #18330](https://github.com/dotnet/fsharp/pull/18330))
9+
* Scoped Nowarn: added the #warnon compiler directive ([Language suggestion #278](https://github.com/fsharp/fslang-suggestions/issues/278), [RFC FS-1146 PR](https://github.com/fsharp/fslang-design/pull/782), [PR #18049](https://github.com/dotnet/fsharp/pull/18049))
910

1011
### Fixed
1112

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5752,7 +5752,7 @@ let CheckOneImplFile
57525752
synImplFile,
57535753
diagnosticOptions) =
57545754

5755-
let (ParsedImplFileInput (fileName, isScript, qualNameOfFile, scopedPragmas, _, implFileFrags, isLastCompiland, _, _)) = synImplFile
5755+
let (ParsedImplFileInput (fileName, isScript, qualNameOfFile, _, implFileFrags, isLastCompiland, _, _)) = synImplFile
57565756
let infoReader = InfoReader(g, amap)
57575757

57585758
cancellable {
@@ -5891,7 +5891,7 @@ let CheckOneImplFile
58915891
|> Array.map (fun (KeyValue(k,v)) -> (k,v))
58925892
|> Map
58935893

5894-
let implFile = CheckedImplFile (qualNameOfFile, scopedPragmas, implFileTy, implFileContents, hasExplicitEntryPoint, isScript, anonRecdTypes, namedDebugPointsForInlinedCode)
5894+
let implFile = CheckedImplFile (qualNameOfFile, implFileTy, implFileContents, hasExplicitEntryPoint, isScript, anonRecdTypes, namedDebugPointsForInlinedCode)
58955895

58965896
return (topAttrs, implFile, envAtEnd, cenv.createsGeneratedProvidedTypes)
58975897
}

src/Compiler/CodeGen/IlxGen.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10391,7 +10391,7 @@ and GenModuleBinding cenv (cgbuf: CodeGenBuffer) (qname: QualifiedNameOfFile) la
1039110391

1039210392
/// Generate the namespace fragments in a single file
1039310393
and GenImplFile cenv (mgbuf: AssemblyBuilder) mainInfoOpt eenv (implFile: CheckedImplFileAfterOptimization) =
10394-
let (CheckedImplFile(qname, _, signature, contents, hasExplicitEntryPoint, isScript, anonRecdTypes, _)) =
10394+
let (CheckedImplFile(qname, signature, contents, hasExplicitEntryPoint, isScript, anonRecdTypes, _)) =
1039510395
implFile.ImplFile
1039610396

1039710397
let optimizeDuringCodeGen = implFile.OptimizeDuringCodeGen

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ type PhasedDiagnostic with
379379
// Level 2
380380
| _ -> 2
381381

382-
member x.IsEnabled(severity, options) =
382+
member private x.IsEnabled(severity, options) =
383383
let level = options.WarnLevel
384384
let specificWarnOn = options.WarnOn
385385
let n = x.Number
@@ -412,19 +412,25 @@ type PhasedDiagnostic with
412412
member x.AdjustSeverity(options, severity) =
413413
let n = x.Number
414414

415-
let warnOff () = List.contains n options.WarnOff
415+
let localWarnon () = WarnScopes.IsWarnon options n x.Range
416+
417+
let localNowarn () = WarnScopes.IsNowarn options n x.Range
418+
419+
let warnOff () =
420+
List.contains n options.WarnOff && not (localWarnon ()) || localNowarn ()
416421

417422
match severity with
418423
| FSharpDiagnosticSeverity.Error -> FSharpDiagnosticSeverity.Error
419424
| FSharpDiagnosticSeverity.Warning when
420425
x.IsEnabled(severity, options)
421426
&& ((options.GlobalWarnAsError && not (warnOff ()))
422-
|| List.contains n options.WarnAsError)
427+
|| List.contains n options.WarnAsError && not (localNowarn ()))
423428
&& not (List.contains n options.WarnAsWarn)
424429
->
425430
FSharpDiagnosticSeverity.Error
431+
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnAsError && not (localNowarn ()) -> FSharpDiagnosticSeverity.Error
426432
| FSharpDiagnosticSeverity.Warning when x.IsEnabled(severity, options) && not (warnOff ()) -> FSharpDiagnosticSeverity.Warning
427-
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnAsError -> FSharpDiagnosticSeverity.Error
433+
| FSharpDiagnosticSeverity.Warning when localWarnon () -> FSharpDiagnosticSeverity.Warning
428434
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnOn && not (warnOff ()) -> FSharpDiagnosticSeverity.Warning
429435
| FSharpDiagnosticSeverity.Info when x.IsEnabled(severity, options) && not (warnOff ()) -> FSharpDiagnosticSeverity.Info
430436
| _ -> FSharpDiagnosticSeverity.Hidden
@@ -2274,51 +2280,25 @@ type PhasedDiagnostic with
22742280
diagnostic.OutputContext(buf, prefix, fileLineFunction)
22752281
diagnostic.Output(buf, tcConfig, severity))
22762282

2277-
//----------------------------------------------------------------------------
2278-
// Scoped #nowarn pragmas
2279-
2280-
/// Build an DiagnosticsLogger that delegates to another DiagnosticsLogger but filters warnings turned off by the given pragma declarations
2281-
//
2282-
// NOTE: we allow a flag to turn of strict file checking. This is because file names sometimes don't match due to use of
2283-
// #line directives, e.g. for pars.fs/pars.fsy. In this case we just test by line number - in most cases this is sufficient
2284-
// because we install a filtering error handler on a file-by-file basis for parsing and type-checking.
2285-
// However this is indicative of a more systematic problem where source-line
2286-
// sensitive operations (lexfilter and warning filtering) do not always
2287-
// interact well with #line directives.
2288-
type DiagnosticsLoggerFilteringByScopedPragmas
2289-
(checkFile, scopedPragmas, diagnosticOptions: FSharpDiagnosticOptions, diagnosticsLogger: DiagnosticsLogger) =
2290-
inherit DiagnosticsLogger("DiagnosticsLoggerFilteringByScopedPragmas")
2283+
/// Build an DiagnosticsLogger that delegates to another DiagnosticsLogger but filters warnings
2284+
type DiagnosticsLoggerFilteringByScopedNowarn(diagnosticOptions: FSharpDiagnosticOptions, diagnosticsLogger: DiagnosticsLogger) =
2285+
inherit DiagnosticsLogger("DiagnosticsLoggerFilteringByScopedNowarn")
22912286

22922287
let mutable realErrorPresent = false
22932288

22942289
override _.DiagnosticSink(diagnostic: PhasedDiagnostic, severity) =
2290+
22952291
if severity = FSharpDiagnosticSeverity.Error then
22962292
realErrorPresent <- true
22972293
diagnosticsLogger.DiagnosticSink(diagnostic, severity)
22982294
else
2299-
let report =
2300-
let warningNum = diagnostic.Number
2301-
2302-
match diagnostic.Range with
2303-
| Some m ->
2304-
scopedPragmas
2305-
|> List.exists (fun pragma ->
2306-
let (ScopedPragma.WarningOff(pragmaRange, warningNumFromPragma)) = pragma
2307-
2308-
warningNum = warningNumFromPragma
2309-
&& (not checkFile || m.FileIndex = pragmaRange.FileIndex)
2310-
&& posGeq m.Start pragmaRange.Start)
2311-
|> not
2312-
| None -> true
2313-
2314-
if report then
2315-
match diagnostic.AdjustSeverity(diagnosticOptions, severity) with
2316-
| FSharpDiagnosticSeverity.Hidden -> ()
2317-
| s -> diagnosticsLogger.DiagnosticSink(diagnostic, s)
2295+
match diagnostic.AdjustSeverity(diagnosticOptions, severity) with
2296+
| FSharpDiagnosticSeverity.Hidden -> ()
2297+
| s -> diagnosticsLogger.DiagnosticSink(diagnostic, s)
23182298

23192299
override _.ErrorCount = diagnosticsLogger.ErrorCount
23202300

23212301
override _.CheckForRealErrorsIgnoringWarnings = realErrorPresent
23222302

2323-
let GetDiagnosticsLoggerFilteringByScopedPragmas (checkFile, scopedPragmas, diagnosticOptions, diagnosticsLogger) =
2324-
DiagnosticsLoggerFilteringByScopedPragmas(checkFile, scopedPragmas, diagnosticOptions, diagnosticsLogger) :> DiagnosticsLogger
2303+
let GetDiagnosticsLoggerFilteringByScopedNowarn (diagnosticOptions, diagnosticsLogger) =
2304+
DiagnosticsLoggerFilteringByScopedNowarn(diagnosticOptions, diagnosticsLogger) :> DiagnosticsLogger

src/Compiler/Driver/CompilerDiagnostics.fsi

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,8 @@ type PhasedDiagnostic with
7777
unit
7878

7979
/// Get a diagnostics logger that filters the reporting of warnings based on scoped pragma information
80-
val GetDiagnosticsLoggerFilteringByScopedPragmas:
81-
checkFile: bool *
82-
scopedPragmas: ScopedPragma list *
83-
diagnosticOptions: FSharpDiagnosticOptions *
84-
diagnosticsLogger: DiagnosticsLogger ->
85-
DiagnosticsLogger
80+
val GetDiagnosticsLoggerFilteringByScopedNowarn:
81+
diagnosticOptions: FSharpDiagnosticOptions * diagnosticsLogger: DiagnosticsLogger -> DiagnosticsLogger
8682

8783
/// Remove 'implicitIncludeDir' from a file name before output
8884
val SanitizeFileName: fileName: string -> implicitIncludeDir: string -> string

0 commit comments

Comments
 (0)