From 77f3d7b45b8536d311482046d53a6bbafbe8d1a3 Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Fri, 29 Sep 2023 16:39:16 +0000 Subject: [PATCH 1/3] Warn if function shadows union case (Fix for #15559, improve error reporting) --- src/Compiler/Checking/CheckExpressions.fs | 12 ++++++- src/Compiler/FSComp.txt | 2 ++ src/Compiler/Facilities/DiagnosticsLogger.fs | 2 ++ src/Compiler/Facilities/LanguageFeatures.fs | 3 ++ src/Compiler/Facilities/LanguageFeatures.fsi | 1 + src/Compiler/Service/ServiceLexing.fs | 2 ++ src/Compiler/xlf/FSComp.txt.cs.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.de.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.es.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.fr.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.it.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.ja.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.ko.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.pl.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.pt-BR.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.ru.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.tr.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.zh-Hans.xlf | 10 ++++++ src/Compiler/xlf/FSComp.txt.zh-Hant.xlf | 10 ++++++ .../LetBindings/Basic/Basic.fs | 35 +++++++++++++++++++ tests/service/data/TestTP/ProvidedTypes.fs | 1 + 21 files changed, 187 insertions(+), 1 deletion(-) diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index c52fb1cf8aa..9c29719e46d 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -10758,7 +10758,17 @@ and TcNonrecBindingTyparDecls cenv env tpenv bind = let (NormalizedBinding(_, _, _, _, _, _, synTyparDecls, _, _, _, _, _)) = bind TcBindingTyparDecls true cenv env tpenv synTyparDecls -and TcNonRecursiveBinding declKind cenv env tpenv ty binding = +and TcNonRecursiveBinding declKind (cenv: cenv) env tpenv ty binding = + // Check for unintended shadowing + if cenv.g.langVersion.SupportsFeature (LanguageFeature.WarnIfFunctionShadowsUnionCase) then + match binding with + | SynBinding(headPat = SynPat.LongIdent(longDotId = SynLongIdent(id = [ident]); range = headPatRange)) -> + match env.eNameResEnv.ePatItems.TryFind ident.idText with + | Some (Item.UnionCase(_, false)) -> + warning(Error(FSComp.SR.tcWarnIfFunctionShadowsUnionCase(), headPatRange)) + | _ -> () + | _ -> () + let binding = BindingNormalization.NormalizeBinding ValOrMemberBinding cenv env binding let explicitTyparInfo, tpenv = TcNonrecBindingTyparDecls cenv env tpenv binding TcNormalizedBinding declKind cenv env tpenv ty None NoSafeInitInfo ([], explicitTyparInfo) binding diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt index 9f091d07533..077e3375a04 100644 --- a/src/Compiler/FSComp.txt +++ b/src/Compiler/FSComp.txt @@ -1588,6 +1588,7 @@ featureChkNotTailRecursive,"Raises warnings if a member or function has the 'Tai featureWhileBang,"'while!' expression" featureExtendedFixedBindings,"extended fixed bindings for byref and GetPinnableReference" featurePreferStringGetPinnableReference,"prefer String.GetPinnableReference in fixed bindings" +featureWarnIfFunctionShadowsUnionCase,"Raises a warning when a function definition shadows a union case" 3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation." 3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation." 3355,tcNotAnIndexerNamedIndexingNotYetEnabled,"The value '%s' is not a function and does not support index notation." @@ -1724,3 +1725,4 @@ featureUnmanagedConstraintCsharpInterop,"Interop between C#'s and F#'s unmanaged 3578,chkCopyUpdateSyntaxInAnonRecords,"This expression is an anonymous record, use {{|...|}} instead of {{...}}." 3579,alwaysUseTypedStringInterpolation,"Interpolated string contains untyped identifiers. Adding typed format specifiers is recommended." 3580,tcUnexpectedFunTypeInUnionCaseField,"Unexpected function type in union case field definition. If you intend the field to be a function, consider wrapping the function signature with parens, e.g. | Case of a -> b into | Case of (a -> b)." +3582,tcWarnIfFunctionShadowsUnionCase,"This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses." diff --git a/src/Compiler/Facilities/DiagnosticsLogger.fs b/src/Compiler/Facilities/DiagnosticsLogger.fs index b1edce3f820..2778e5c75ae 100644 --- a/src/Compiler/Facilities/DiagnosticsLogger.fs +++ b/src/Compiler/Facilities/DiagnosticsLogger.fs @@ -13,6 +13,8 @@ open System.Threading open Internal.Utilities.Library open Internal.Utilities.Library.Extras +#nowarn "3582" // Remove this line when the Error function is removed as planned below + /// Represents the style being used to format errors [] type DiagnosticStyle = diff --git a/src/Compiler/Facilities/LanguageFeatures.fs b/src/Compiler/Facilities/LanguageFeatures.fs index 0cc5370d570..5a86433ca20 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fs +++ b/src/Compiler/Facilities/LanguageFeatures.fs @@ -79,6 +79,7 @@ type LanguageFeature = | WhileBang | ExtendedFixedBindings | PreferStringGetPinnableReference + | WarnIfFunctionShadowsUnionCase /// LanguageVersion management type LanguageVersion(versionText) = @@ -185,6 +186,7 @@ type LanguageVersion(versionText) = // F# preview LanguageFeature.FromEndSlicing, previewVersion LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion + LanguageFeature.WarnIfFunctionShadowsUnionCase, previewVersion ] static let defaultLanguageVersion = LanguageVersion("default") @@ -321,6 +323,7 @@ type LanguageVersion(versionText) = | LanguageFeature.WhileBang -> FSComp.SR.featureWhileBang () | LanguageFeature.ExtendedFixedBindings -> FSComp.SR.featureExtendedFixedBindings () | LanguageFeature.PreferStringGetPinnableReference -> FSComp.SR.featurePreferStringGetPinnableReference () + | LanguageFeature.WarnIfFunctionShadowsUnionCase -> FSComp.SR.featureWarnIfFunctionShadowsUnionCase () /// Get a version string associated with the given feature. static member GetFeatureVersionString feature = diff --git a/src/Compiler/Facilities/LanguageFeatures.fsi b/src/Compiler/Facilities/LanguageFeatures.fsi index c1cb2378d4e..7672c151ea3 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fsi +++ b/src/Compiler/Facilities/LanguageFeatures.fsi @@ -69,6 +69,7 @@ type LanguageFeature = | WhileBang | ExtendedFixedBindings | PreferStringGetPinnableReference + | WarnIfFunctionShadowsUnionCase /// LanguageVersion management type LanguageVersion = diff --git a/src/Compiler/Service/ServiceLexing.fs b/src/Compiler/Service/ServiceLexing.fs index 13be7d68ffe..30d41fffd85 100644 --- a/src/Compiler/Service/ServiceLexing.fs +++ b/src/Compiler/Service/ServiceLexing.fs @@ -21,6 +21,8 @@ open FSharp.Compiler.Text open FSharp.Compiler.Text.Position open FSharp.Compiler.Text.Range +#nowarn "3582" // Don't complain about shadowing the token cases by their tags + module FSharpTokenTag = let Identifier = tagOfToken (IDENT "a") diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf index 950c8d3c846..52474f0a79e 100644 --- a/src/Compiler/xlf/FSComp.txt.cs.xlf +++ b/src/Compiler/xlf/FSComp.txt.cs.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Vyvolá upozornění, když výraz záznamu kopírování a aktualizace změní všechna pole záznamu. @@ -1387,6 +1392,11 @@ Deklarování \"interfaces with static abstract methods\" (rozhraní se statickými abstraktními metodami) je pokročilá funkce. Pokyny najdete v https://aka.ms/fsharp-iwsams. Toto upozornění můžete zakázat pomocí #nowarn \"3535\" nebo '--nowarn:3535'. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. Člen rozhraní {0} nemá nejvíce specifickou implementaci. diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf index 228da2df3b1..24975aa81f6 100644 --- a/src/Compiler/xlf/FSComp.txt.de.xlf +++ b/src/Compiler/xlf/FSComp.txt.de.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Löst Warnungen aus, wenn ein Ausdruck zum Kopieren und Aktualisieren von Datensätzen alle Felder eines Datensatzes ändert. @@ -1387,6 +1392,11 @@ Das Deklarieren von \"Schnittstellen mit statischen abstrakten Methoden\" ist ein erweitertes Feature. Anleitungen finden Sie unter https://aka.ms/fsharp-iwsams. Sie können diese Warnung deaktivieren, indem Sie „#nowarn \"3535\"“ or „--nowarn:3535“ verwenden. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. Der Schnittstellenmember "{0}" weist keine spezifischste Implementierung auf. diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf index 7db7543091d..00242d91ee9 100644 --- a/src/Compiler/xlf/FSComp.txt.es.xlf +++ b/src/Compiler/xlf/FSComp.txt.es.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Emite advertencias cuando una expresión de copiar y actualizar registros cambia todos los campos de un registro. @@ -1387,6 +1392,11 @@ Declarar \"interfaces con métodos abstractos estáticos\" es una característica avanzada. Consulte https://aka.ms/fsharp-iwsams para obtener instrucciones. Puede deshabilitar esta advertencia con "#nowarn \"3535\"" o "--nowarn:3535". + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. El miembro de interfaz "{0}" no tiene una implementación más específica. diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf index a00e94a4d64..4872e1d3756 100644 --- a/src/Compiler/xlf/FSComp.txt.fr.xlf +++ b/src/Compiler/xlf/FSComp.txt.fr.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Génère des avertissements lorsqu'une expression d'enregistrement de copie et de mise à jour modifie tous les champs d'un enregistrement. @@ -1387,6 +1392,11 @@ La déclaration de \"interfaces with static abstract methods\" est une fonctionnalité avancée. Consultez https://aka.ms/fsharp-iwsams pour obtenir de l’aide. Vous pouvez désactiver cet avertissement à l’aide de '#nowarn \"3535\"' or '--nowarn:3535'. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. Le membre d'interface '{0}' n'a pas l'implémentation la plus spécifique. diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf index a70f3500a2a..fcaeb346b04 100644 --- a/src/Compiler/xlf/FSComp.txt.it.xlf +++ b/src/Compiler/xlf/FSComp.txt.it.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Genera avvisi quando un'espressione di record di copia e aggiornamento modifica tutti i campi di un record. @@ -1387,6 +1392,11 @@ La dichiarazione di \"interfaces with static abstract methods\" è una funzionalità avanzata. Per indicazioni, vedere https://aka.ms/fsharp-iwsams. È possibile disabilitare questo avviso usando '#nowarn \"3535\"' o '--nowarn:3535'. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. Il membro di interfaccia '{0}' non contiene un'implementazione più specifica. diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf index 85b79f2aa38..8dd2cd8ff3b 100644 --- a/src/Compiler/xlf/FSComp.txt.ja.xlf +++ b/src/Compiler/xlf/FSComp.txt.ja.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. copy-and-update レコード式によってレコードのすべてのフィールドが変更されたときに警告を表示します。 @@ -1387,6 +1392,11 @@ \"interfaces with static abstract method\" の宣言は高度な機能です。ガイダンスについては https://aka.ms/fsharp-iwsams を参照してください。この警告は、'#nowarn \"3535\"' または '--nowarn:3535' を使用して無効にできます。 + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. インターフェイス メンバー '{0}' には最も固有な実装がありません。 diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf index fbedcdb01c7..734ef23dd5e 100644 --- a/src/Compiler/xlf/FSComp.txt.ko.xlf +++ b/src/Compiler/xlf/FSComp.txt.ko.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. 레코드 복사 및 업데이트 식이 레코드의 모든 필드를 변경할 때 경고를 발생합니다. @@ -1387,6 +1392,11 @@ \"interfaces with static abstract methods\"를 선언하는 것은 고급 기능입니다. 지침은 https://aka.ms/fsharp-iwsams를 참조하세요. '#nowarn \"3535\"' 또는 '--nowarn:3535'를 사용하여 이 경고를 비활성화할 수 있습니다. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. 인터페이스 멤버 '{0}'에 가장 한정적인 구현이 없습니다. diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf index 7bf3c8187d8..ccdd101d420 100644 --- a/src/Compiler/xlf/FSComp.txt.pl.xlf +++ b/src/Compiler/xlf/FSComp.txt.pl.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Zgłasza ostrzeżenia, gdy wyrażenie rekordu kopiowania i aktualizacji zmieni wszystkie pola rekordu. @@ -1387,6 +1392,11 @@ Deklarowanie \"interfejsów ze statycznymi metodami abstrakcyjnymi\" jest funkcją zaawansowaną. Aby uzyskać wskazówki, zobacz https://aka.ms/fsharp-iwsams. To ostrzeżenie można wyłączyć przy użyciu polecenia „#nowarn \"3535\"" lub "--nowarn:3535”. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. Składowa interfejsu „{0}” nie ma najbardziej specyficznej implementacji. diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf index 5e3a492618d..19cb723b264 100644 --- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf +++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Gera avisos quando uma expressão de registro de cópia e atualização altera todos os campos de um registro. @@ -1387,6 +1392,11 @@ Declarando \"interfaces com métodos abstratos estáticos\" é um recurso avançado. Consulte https://aka.ms/fsharp-iwsams para obter diretrizes. Você pode desabilitar esse aviso usando '#nowarn \"3535\"' ou '--nowarn:3535'. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. O membro de interface '{0}' não tem uma implementação mais específica. diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf index 3e50524becd..7a1e262e802 100644 --- a/src/Compiler/xlf/FSComp.txt.ru.xlf +++ b/src/Compiler/xlf/FSComp.txt.ru.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Создает предупреждения, когда выражение копирования и обновления записи изменяет все поля записи. @@ -1387,6 +1392,11 @@ Объявление \"интерфейсов со статическими абстрактными методами\" является расширенной функцией. См. руководство на https://aka.ms/fsharp-iwsams. Это предупреждение можно отключить с помощью используя "#nowarn \"3535\"" or "--nowarn:3535". + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. Элемент интерфейса "{0}" не имеет наиболее конкретной реализации. diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf index 59a6cc71fee..7a75c727276 100644 --- a/src/Compiler/xlf/FSComp.txt.tr.xlf +++ b/src/Compiler/xlf/FSComp.txt.tr.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. Bir kopyalama ve güncelleştirme kayıt ifadesi bir kaydın tüm alanlarını değiştirdiğinde uyarı oluşturur. @@ -1387,6 +1392,11 @@ \"interfaces with static abstract methods\" bildirimi gelişmiş bir özelliktir. Rehber için bkz. https://aka.ms/fsharp-iwsams. '#nowarn \"3535\"' veya '--nowarn:3535'. kullanarak bu uyarıyı devre dışı bırakabilirsiniz. + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. '{0}' arabirim üyesinin en belirgin uygulaması yok. diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf index a836bd75cb1..4a2427dd645 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. 复制和更新记录表达式更改记录的所有字段时引发警告。 @@ -1387,6 +1392,11 @@ 声明“使用静态抽象方法的接口”是一项高级功能。有关指南,请参阅 https://aka.ms/fsharp-iwsams。可以使用 "#nowarn \"3535\"' 或 '--nowarn:3535' 禁用此警告。 + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. 接口成员“{0}”没有最具体的实现。 diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf index 9b7c435824e..583baedc60d 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf @@ -482,6 +482,11 @@ Interop between C#'s and F#'s unmanaged generic constraint (emit additional modreq) + + Raises a warning when a function definition shadows a union case + Raises a warning when a function definition shadows a union case + + Raises warnings when an copy-and-update record expression changes all fields of a record. 當複製和更新記錄運算式變更記錄的所有欄位時引發警告。 @@ -1387,6 +1392,11 @@ 使用「靜態抽象方法宣告介面」是進階的功能。請參閱 https://aka.ms/fsharp-iwsams 以尋求指引。您可以使用 '#nowarn \"3535\"' 或 '--nowarn:3535' 來停用此警告。 + + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses. + + Interface member '{0}' does not have a most specific implementation. 介面成員 '{0}' 沒有最具體的實作。 diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/LetBindings/Basic/Basic.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/LetBindings/Basic/Basic.fs index ea00b30e1be..27ce3e511ed 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/LetBindings/Basic/Basic.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/LetBindings/Basic/Basic.fs @@ -271,3 +271,38 @@ module LetBindings_Basic = compilation |> verifyCompileAndRun |> shouldSucceed + + // https://github.com/dotnet/fsharp/issues/15559 + + let private sourceWarnIfFunctionShadowsUnionCase = + """ +type T = Case1 of int +let t = Case1 42 +let Case1 x = t // first warning +let Some x = 42 // second warning +[] type U = U1 of int +let u = U.U1 42 +let U1 x = t // no warning +type C() = + member _.Some x = 1 // no warning +""" + + [] + let ``Warn if function shadows union case - LangVersion <= 8`` () = + sourceWarnIfFunctionShadowsUnionCase + |> FSharp + |> withLangVersion80 + |> verifyCompileAndRun + |> shouldSucceed + + [] + let ``Warn if function shadows union case - LangVersion > 8`` () = + sourceWarnIfFunctionShadowsUnionCase + |> FSharp + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Warning 3582, Line 4, Col 5, Line 4, Col 12, "This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses.") + (Warning 3582, Line 5, Col 5, Line 5, Col 11, "This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parantheses.") + ] diff --git a/tests/service/data/TestTP/ProvidedTypes.fs b/tests/service/data/TestTP/ProvidedTypes.fs index 4e7a7604c6b..5362e043e55 100644 --- a/tests/service/data/TestTP/ProvidedTypes.fs +++ b/tests/service/data/TestTP/ProvidedTypes.fs @@ -6,6 +6,7 @@ namespace ProviderImplementation.ProvidedTypes #nowarn "1182" #nowarn "3370" +#nowarn "3582" // This file contains a set of helper types and methods for providing types in an implementation // of ITypeProvider. From c074d6cf5db092996cdc689a17b7f7154fbc70f3 Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Fri, 29 Sep 2023 18:32:55 +0000 Subject: [PATCH 2/3] fixed formatting --- src/Compiler/Facilities/DiagnosticsLogger.fs | 2 +- src/Compiler/Service/ServiceLexing.fs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compiler/Facilities/DiagnosticsLogger.fs b/src/Compiler/Facilities/DiagnosticsLogger.fs index 2778e5c75ae..16ac0929f11 100644 --- a/src/Compiler/Facilities/DiagnosticsLogger.fs +++ b/src/Compiler/Facilities/DiagnosticsLogger.fs @@ -13,7 +13,7 @@ open System.Threading open Internal.Utilities.Library open Internal.Utilities.Library.Extras -#nowarn "3582" // Remove this line when the Error function is removed as planned below +#nowarn "3582" // Remove this line when the Error function is removed as planned below /// Represents the style being used to format errors [] diff --git a/src/Compiler/Service/ServiceLexing.fs b/src/Compiler/Service/ServiceLexing.fs index 30d41fffd85..fa0a627bc87 100644 --- a/src/Compiler/Service/ServiceLexing.fs +++ b/src/Compiler/Service/ServiceLexing.fs @@ -21,7 +21,7 @@ open FSharp.Compiler.Text open FSharp.Compiler.Text.Position open FSharp.Compiler.Text.Range -#nowarn "3582" // Don't complain about shadowing the token cases by their tags +#nowarn "3582" // Don't complain about shadowing the token cases by their tags module FSharpTokenTag = From 5a33443339a6514007aa07dde13ad5a875efab47 Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Fri, 29 Sep 2023 20:23:04 +0000 Subject: [PATCH 3/3] added #nowarn to ProvidedTypes.fs in vs integration tests --- .../DummyProviderForLanguageServiceTesting/ProvidedTypes.fs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vsintegration/tests/MockTypeProviders/DummyProviderForLanguageServiceTesting/ProvidedTypes.fs b/vsintegration/tests/MockTypeProviders/DummyProviderForLanguageServiceTesting/ProvidedTypes.fs index ae593e9961a..3534c4d33de 100644 --- a/vsintegration/tests/MockTypeProviders/DummyProviderForLanguageServiceTesting/ProvidedTypes.fs +++ b/vsintegration/tests/MockTypeProviders/DummyProviderForLanguageServiceTesting/ProvidedTypes.fs @@ -5,6 +5,8 @@ namespace ProviderImplementation.ProvidedTypes #nowarn "1182" + #nowarn "3582" + // This file contains a set of helper types and methods for providing types in an implementation // of ITypeProvider.