Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 16e59f4

Browse files
authored
Revert redefined function diagnostic (microsoft#1359)
1 parent 1bcba75 commit 16e59f4

File tree

5 files changed

+15
-384
lines changed

5 files changed

+15
-384
lines changed

src/Analysis/Ast/Impl/Analyzer/Symbols/SymbolCollector.cs

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,19 @@ private PythonClassType CreateClass(ClassDefinition cd) {
9999

100100
private void AddFunctionOrProperty(FunctionDefinition fd) {
101101
var declaringType = fd.Parent != null && _typeMap.TryGetValue(fd.Parent, out var t) ? t : null;
102-
var existing = _eval.LookupNameInScopes(fd.Name, LookupOptions.Local);
103-
104-
switch (existing?.MemberType) {
105-
case PythonMemberType.Method:
106-
case PythonMemberType.Function:
107-
case PythonMemberType.Property:
108-
ReportRedefinedFunction(fd, existing as PythonType);
109-
break;
110-
}
111-
112-
if (!TryAddProperty(fd, existing, declaringType)) {
113-
AddFunction(fd, existing, declaringType);
102+
if (!TryAddProperty(fd, declaringType)) {
103+
AddFunction(fd, declaringType);
114104
}
115105
}
116106

117-
private void AddFunction(FunctionDefinition fd, IMember existing, IPythonType declaringType) {
118-
if (!(existing is PythonFunctionType f)) {
119-
f = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
107+
private void AddFunction(FunctionDefinition fd, IPythonType declaringType) {
108+
if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonFunctionType existing)) {
109+
existing = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
120110
// The variable is transient (non-user declared) hence it does not have location.
121111
// Function type is tracking locations for references and renaming.
122-
_eval.DeclareVariable(fd.Name, f, VariableSource.Declaration);
112+
_eval.DeclareVariable(fd.Name, existing, VariableSource.Declaration);
123113
}
124-
AddOverload(fd, f, o => f.AddOverload(o));
114+
AddOverload(fd, existing, o => existing.AddOverload(o));
125115
}
126116

127117
private void AddOverload(FunctionDefinition fd, IPythonClassMember function, Action<IPythonFunctionOverload> addOverload) {
@@ -159,31 +149,31 @@ private PythonFunctionOverload GetOverloadFromStub(FunctionDefinition node) {
159149
return null;
160150
}
161151

162-
private bool TryAddProperty(FunctionDefinition node, IMember existing, IPythonType declaringType) {
152+
private bool TryAddProperty(FunctionDefinition node, IPythonType declaringType) {
163153
var dec = node.Decorators?.Decorators;
164154
var decorators = dec != null ? dec.ExcludeDefault().ToArray() : Array.Empty<Expression>();
165155

166156
foreach (var d in decorators.OfType<NameExpression>()) {
167157
switch (d.Name) {
168158
case @"property":
169-
AddProperty(node, existing, declaringType, false);
159+
AddProperty(node, declaringType, false);
170160
return true;
171161
case @"abstractproperty":
172-
AddProperty(node, existing, declaringType, true);
162+
AddProperty(node, declaringType, true);
173163
return true;
174164
}
175165
}
176166
return false;
177167
}
178168

179-
private void AddProperty(FunctionDefinition fd, IMember existing, IPythonType declaringType, bool isAbstract) {
180-
if (!(existing is PythonPropertyType p)) {
181-
p = new PythonPropertyType(fd, _eval.GetLocationOfName(fd), declaringType, isAbstract);
169+
private void AddProperty(FunctionDefinition fd, IPythonType declaringType, bool isAbstract) {
170+
if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonPropertyType existing)) {
171+
existing = new PythonPropertyType(fd, _eval.GetLocationOfName(fd), declaringType, isAbstract);
182172
// The variable is transient (non-user declared) hence it does not have location.
183173
// Property type is tracking locations for references and renaming.
184-
_eval.DeclareVariable(fd.Name, p, VariableSource.Declaration);
174+
_eval.DeclareVariable(fd.Name, existing, VariableSource.Declaration);
185175
}
186-
AddOverload(fd, p, o => p.AddOverload(o));
176+
AddOverload(fd, existing, o => existing.AddOverload(o));
187177
}
188178

189179
private IMember GetMemberFromStub(string name) {
@@ -213,22 +203,6 @@ private IMember GetMemberFromStub(string name) {
213203
return member;
214204
}
215205

216-
private void ReportRedefinedFunction(FunctionDefinition redefined, ILocatedMember existing) {
217-
// get line number of existing function for diagnostic message
218-
var existingLoc = existing.Definition;
219-
var existingLine = existingLoc.Span.Start.Line;
220-
221-
_eval.ReportDiagnostics(
222-
_eval.Module.Uri,
223-
new DiagnosticsEntry(
224-
Resources.FunctionRedefined.FormatInvariant(existingLine),
225-
// only highlight the redefined name
226-
_eval.GetLocationInfo(redefined.NameExpression).Span,
227-
ErrorCodes.FunctionRedefined,
228-
Parsing.Severity.Error,
229-
DiagnosticSource.Analysis));
230-
}
231-
232206
private static bool IsDeprecated(ClassDefinition cd)
233207
=> cd.Decorators?.Decorators != null && IsDeprecated(cd.Decorators.Decorators);
234208

src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public static class ErrorCodes {
2525
public const string UndefinedVariable = "undefined-variable";
2626
public const string VariableNotDefinedGlobally= "variable-not-defined-globally";
2727
public const string VariableNotDefinedNonLocal = "variable-not-defined-nonlocal";
28-
public const string FunctionRedefined = "function-redefined";
29-
public const string UnsupportedOperandType = "unsupported-operand-type";
3028
public const string ReturnInInit = "return-in-init";
3129
public const string TypingTypeVarArguments = "typing-typevar-arguments";
3230
public const string TypingNewTypeArguments = "typing-newtype-arguments";

src/Analysis/Ast/Impl/Resources.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analysis/Ast/Impl/Resources.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@
195195
<data name="TypeVarSingleConstraint" xml:space="preserve">
196196
<value>A single constraint to TypeVar is not allowed.</value>
197197
</data>
198-
<data name="FunctionRedefined" xml:space="preserve">
199-
<value>Function already defined at line {0}.</value>
200-
</data>
201198
<data name="NewTypeFirstArgNotString" xml:space="preserve">
202199
<value>The first argument to NewType must be a string, but it is of type '{0}'.</value>
203200
</data>

0 commit comments

Comments
 (0)