Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Revert redefined function diagnostic #1359

Merged
merged 1 commit into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 15 additions & 41 deletions src/Analysis/Ast/Impl/Analyzer/Symbols/SymbolCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,19 @@ private PythonClassType CreateClass(ClassDefinition cd) {

private void AddFunctionOrProperty(FunctionDefinition fd) {
var declaringType = fd.Parent != null && _typeMap.TryGetValue(fd.Parent, out var t) ? t : null;
var existing = _eval.LookupNameInScopes(fd.Name, LookupOptions.Local);

switch (existing?.MemberType) {
case PythonMemberType.Method:
case PythonMemberType.Function:
case PythonMemberType.Property:
ReportRedefinedFunction(fd, existing as PythonType);
break;
}

if (!TryAddProperty(fd, existing, declaringType)) {
AddFunction(fd, existing, declaringType);
if (!TryAddProperty(fd, declaringType)) {
AddFunction(fd, declaringType);
}
}

private void AddFunction(FunctionDefinition fd, IMember existing, IPythonType declaringType) {
if (!(existing is PythonFunctionType f)) {
f = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
private void AddFunction(FunctionDefinition fd, IPythonType declaringType) {
if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonFunctionType existing)) {
existing = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
// The variable is transient (non-user declared) hence it does not have location.
// Function type is tracking locations for references and renaming.
_eval.DeclareVariable(fd.Name, f, VariableSource.Declaration);
_eval.DeclareVariable(fd.Name, existing, VariableSource.Declaration);
}
AddOverload(fd, f, o => f.AddOverload(o));
AddOverload(fd, existing, o => existing.AddOverload(o));
}

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

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

foreach (var d in decorators.OfType<NameExpression>()) {
switch (d.Name) {
case @"property":
AddProperty(node, existing, declaringType, false);
AddProperty(node, declaringType, false);
return true;
case @"abstractproperty":
AddProperty(node, existing, declaringType, true);
AddProperty(node, declaringType, true);
return true;
}
}
return false;
}

private void AddProperty(FunctionDefinition fd, IMember existing, IPythonType declaringType, bool isAbstract) {
if (!(existing is PythonPropertyType p)) {
p = new PythonPropertyType(fd, _eval.GetLocationOfName(fd), declaringType, isAbstract);
private void AddProperty(FunctionDefinition fd, IPythonType declaringType, bool isAbstract) {
if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonPropertyType existing)) {
existing = new PythonPropertyType(fd, _eval.GetLocationOfName(fd), declaringType, isAbstract);
// The variable is transient (non-user declared) hence it does not have location.
// Property type is tracking locations for references and renaming.
_eval.DeclareVariable(fd.Name, p, VariableSource.Declaration);
_eval.DeclareVariable(fd.Name, existing, VariableSource.Declaration);
}
AddOverload(fd, p, o => p.AddOverload(o));
AddOverload(fd, existing, o => existing.AddOverload(o));
}

private IMember GetMemberFromStub(string name) {
Expand Down Expand Up @@ -213,22 +203,6 @@ private IMember GetMemberFromStub(string name) {
return member;
}

private void ReportRedefinedFunction(FunctionDefinition redefined, ILocatedMember existing) {
// get line number of existing function for diagnostic message
var existingLoc = existing.Definition;
var existingLine = existingLoc.Span.Start.Line;

_eval.ReportDiagnostics(
_eval.Module.Uri,
new DiagnosticsEntry(
Resources.FunctionRedefined.FormatInvariant(existingLine),
// only highlight the redefined name
_eval.GetLocationInfo(redefined.NameExpression).Span,
ErrorCodes.FunctionRedefined,
Parsing.Severity.Error,
DiagnosticSource.Analysis));
}

private static bool IsDeprecated(ClassDefinition cd)
=> cd.Decorators?.Decorators != null && IsDeprecated(cd.Decorators.Decorators);

Expand Down
2 changes: 0 additions & 2 deletions src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public static class ErrorCodes {
public const string UndefinedVariable = "undefined-variable";
public const string VariableNotDefinedGlobally= "variable-not-defined-globally";
public const string VariableNotDefinedNonLocal = "variable-not-defined-nonlocal";
public const string FunctionRedefined = "function-redefined";
public const string UnsupportedOperandType = "unsupported-operand-type";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in #1273 but was a merge error there; removing it now too.

public const string ReturnInInit = "return-in-init";
public const string TypingTypeVarArguments = "typing-typevar-arguments";
public const string TypingNewTypeArguments = "typing-newtype-arguments";
Expand Down
9 changes: 0 additions & 9 deletions src/Analysis/Ast/Impl/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/Analysis/Ast/Impl/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@
<data name="TypeVarSingleConstraint" xml:space="preserve">
<value>A single constraint to TypeVar is not allowed.</value>
</data>
<data name="FunctionRedefined" xml:space="preserve">
<value>Function already defined at line {0}.</value>
</data>
<data name="NewTypeFirstArgNotString" xml:space="preserve">
<value>The first argument to NewType must be a string, but it is of type '{0}'.</value>
</data>
Expand Down
Loading