Skip to content

Commit

Permalink
scoped nowarn for signature files
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin521 committed Aug 11, 2024
1 parent 5be6bb5 commit 1cdcd07
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 60 deletions.
50 changes: 15 additions & 35 deletions src/Compiler/Driver/ParseAndCheckInputs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -271,29 +271,6 @@ let GetScopedPragmas (langVersion: LanguageVersion) hashDirectives =
|> List.fold processWarnDirectiveInfo (Map.empty, [])
|> addOpenPragmas

let GetScopedPragmasForHashDirective hd (langVersion: LanguageVersion) =
let supportsNonStringArguments =
langVersion.SupportsFeature(LanguageFeature.ParsedHashDirectiveArgumentNonQuotes)

[
match hd with
| ParsedHashDirective("nowarn", numbers, m) ->
for s in numbers do
let warningNumber =
match supportsNonStringArguments, s with
| _, ParsedHashDirectiveArgument.SourceIdentifier _ -> None
| true, ParsedHashDirectiveArgument.LongIdent _ -> None
| true, ParsedHashDirectiveArgument.Int32(n, _) -> GetWarningNumber(m, string n, true)
| true, ParsedHashDirectiveArgument.Ident(s, _) -> GetWarningNumber(m, s.idText, true)
| _, ParsedHashDirectiveArgument.String(s, _, _) -> GetWarningNumber(m, s, true)
| _ -> None

match warningNumber with
| None -> ()
| Some n -> ScopedPragma.WarningOff(m, n)
| _ -> ()
]

let private collectCodeComments (lexbuf: UnicodeLexing.Lexbuf) (tripleSlashComments: range list) =
[
yield! LexbufCommentStore.GetComments(lexbuf)
Expand All @@ -308,7 +285,7 @@ let PostParseModuleImpls
defaultNamespace,
fileName,
isLastCompiland,
ParsedImplFile(toplevelHashDirectives, impls),
ParsedImplFile(toplevelHashDirectives, impls), // Are toplevelHashDirectives ever non-empty??
lexbuf: UnicodeLexing.Lexbuf,
tripleSlashComments: range list,
identifiers: Set<string>
Expand Down Expand Up @@ -373,7 +350,7 @@ let PostParseModuleSpecs
defaultNamespace,
fileName,
isLastCompiland,
ParsedSigFile(hashDirectives, specs),
ParsedSigFile(toplevelHashDirectives, specs), // Can toplevelHashDirectives ever be non-empty??
lexbuf: UnicodeLexing.Lexbuf,
tripleSlashComments: range list,
identifiers: Set<string>
Expand All @@ -396,15 +373,18 @@ let PostParseModuleSpecs
let qualName = QualFileNameOfSpecs fileName specs

let scopedPragmas =
[
for SynModuleOrNamespaceSig(decls = decls) in specs do
for d in decls do
match d with
| SynModuleSigDecl.HashDirective(hd, _) -> yield! GetScopedPragmasForHashDirective hd lexbuf.LanguageVersion
| _ -> ()
for hd in hashDirectives do
yield! GetScopedPragmasForHashDirective hd lexbuf.LanguageVersion
]
let hashDirectives =
let getModuleSigHashDirectives (SynModuleOrNamespaceSig(decls = decls)) =
let getModuleDeclSigHashDirectives decl =
match decl with
| SynModuleSigDecl.HashDirective(hd, _) -> Some hd
| _ -> None

decls |> List.choose getModuleDeclSigHashDirectives

(specs |> List.collect getModuleSigHashDirectives) @ toplevelHashDirectives

hashDirectives |> GetScopedPragmas lexbuf.LanguageVersion

let conditionalDirectives = LexbufIfdefStore.GetTrivia(lexbuf)
let codeComments = collectCodeComments lexbuf tripleSlashComments
Expand All @@ -415,7 +395,7 @@ let PostParseModuleSpecs
CodeComments = codeComments
}

ParsedInput.SigFile(ParsedSigFileInput(fileName, qualName, scopedPragmas, hashDirectives, specs, trivia, identifiers))
ParsedInput.SigFile(ParsedSigFileInput(fileName, qualName, scopedPragmas, toplevelHashDirectives, specs, trivia, identifiers))

type ModuleNamesDict = Map<string, Map<string, QualifiedNameOfFile>>

Expand Down
62 changes: 37 additions & 25 deletions tests/FSharp.Compiler.ComponentTests/CompilerDirectives/Nowarn.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ open FSharp.Test.Compiler
module Nowarn =

let private warning25Text = "Incomplete pattern matches on this expression. For example, the value 'Some (_)' may indicate a case not covered by the pattern(s)."
let private error3350Text = "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 'PREVIEW' or greater."
let private warning44Text = "This construct is deprecated"

let private sourceForWarningIsSuppressed = """
Expand All @@ -30,29 +29,7 @@ match None with None -> ()
Warning 25, Line 7, Col 7, Line 7, Col 11, warning25Text
]

let private scriptForWarningIsSuppressed = """
match None with None -> ()
#nowarn "25"
match None with None -> ()
#warnon "25"
match None with None -> ()
#nowarn "25"
match None with None -> ()
"""

[<Fact>]
let ``warning is suppressed between nowarn and warnon directives (in script)`` () =
Fsx scriptForWarningIsSuppressed
|> withLangVersionPreview
|> compile
|> withDiagnostics [
// These warnings should appear if we make scripts spec-compliant
// Warning 25, Line 3, Col 7, Line 3, Col 11, matchNoneErrorMessage
// Warning 25, Line 7, Col 7, Line 7, Col 11, matchNoneErrorMessage
]

let private sourceForWarningIsSuppressedInSigFile = """
let private sigSourceForWarningIsSuppressedInSigFile = """
module A
open System
[<Obsolete>]
Expand All @@ -66,12 +43,47 @@ type T4 = T
type T5 = T
"""

let private sourceForWarningIsSuppressedInSigFile = """
module A
#nowarn "44"
open System
[<Obsolete>]
type T = class end
type T2 = T
type T3 = T
type T4 = T
type T5 = T
"""

[<Fact>]
let ``warning is suppressed between nowarn and warnon directives in a signature file`` () =
Fsi sourceForWarningIsSuppressedInSigFile
Fsi sigSourceForWarningIsSuppressedInSigFile
|> withAdditionalSourceFile (FsSource sourceForWarningIsSuppressedInSigFile)
|> withLangVersionPreview
|> compile
|> withDiagnostics [
Warning 44, Line 6, Col 11, Line 6, Col 12, warning44Text
Warning 44, Line 10, Col 11, Line 10, Col 12, warning44Text
]

let private scriptForWarningIsSuppressed = """
match None with None -> ()
#nowarn "25"
match None with None -> ()
#warnon "25"
match None with None -> ()
#nowarn "25"
match None with None -> ()
"""

[<Fact>]
let ``warning is suppressed between nowarn and warnon directives (in script)`` () =
Fsx scriptForWarningIsSuppressed
|> withLangVersionPreview
|> compile
|> withDiagnostics [
// These warnings should appear if we make scripts spec-compliant
// Warning 25, Line 3, Col 7, Line 3, Col 11, matchNoneErrorMessage
// Warning 25, Line 7, Col 7, Line 7, Col 11, matchNoneErrorMessage
]

0 comments on commit 1cdcd07

Please sign in to comment.