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

Bug fixes #418

Merged
merged 10 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Collections.Generic;
using Microsoft.PythonTools.Analysis;
using Microsoft.PythonTools.Analysis.Infrastructure;

namespace Microsoft.PythonTools.Interpreter.Ast {
class AstPythonBoundMethod : IPythonBoundFunction, ILocatedMember {
Expand All @@ -27,6 +28,6 @@ public AstPythonBoundMethod(IPythonFunction function, IPythonType selfType) {
public IPythonFunction Function { get; }
public IPythonType SelfType { get; }
public PythonMemberType MemberType => PythonMemberType.Method;
public IEnumerable<ILocationInfo> Locations => (Function as ILocatedMember)?.Locations;
public IEnumerable<ILocationInfo> Locations => (Function as ILocatedMember)?.Locations.MaybeEnumerate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public string Documentation {
public Uri DocumentUri { get; }
public PythonMemberType MemberType => PythonMemberType.Module;
public Dictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public IEnumerable<ILocationInfo> Locations { get; }
public IEnumerable<ILocationInfo> Locations { get; } = Enumerable.Empty<ILocationInfo>();

public int AnalysisVersion => 1;
public IModuleContext AnalysisContext => null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public AstPythonProperty(
ILocationInfo location
) {
IsReadOnly = true;
Locations = new[] { location };
Locations = location != null ? new[] { location } : Enumerable.Empty<ILocationInfo>();
FunctionDefinition = definition;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private AstPythonType(string name, Dictionary<string, IMember> members, IEnumera
_members = members;
_mro = Array.Empty<IPythonType>();
DeclaringModule = NoDeclModule;
Locations = locations;
Locations = locations ?? Enumerable.Empty<ILocationInfo>();
}

internal void AddMembers(IEnumerable<KeyValuePair<string, IMember>> members, bool overwrite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public virtual IEnumerable<string> GetMemberNames(IModuleContext moduleContext)
internal PythonAst Ast { get; private set; }

#if DEBUG
public IEnumerable<ILocationInfo> Locations { get; private set; } = new LocationInfo[0];
public IEnumerable<ILocationInfo> Locations { get; private set; } = Enumerable.Empty<ILocationInfo>();
#endif

protected virtual List<string> GetScrapeArguments(IPythonInterpreterFactory factory) {
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/MemberResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal MemberResult(string name, Func<IEnumerable<AnalysisValue>> vars, Func<P
///
/// New in 1.5.
/// </summary>
public IEnumerable<ILocationInfo> Locations => Values.SelectMany(ns => ns.Locations);
public IEnumerable<ILocationInfo> Locations => Values.SelectMany(ns => ns.Locations.MaybeEnumerate());

public IEnumerable<AnalysisValue> Values => _vars.Value;

Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/ModuleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ internal override bool ModuleContainsMember(IModuleContext context, string name)
return false;
}

foreach (var location in type.Locations) {
foreach (var location in type.Locations.WhereNotNull()) {
if (location.FilePath != modInfo.ProjectEntry.FilePath) {
// declared in another module
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Engine/Impl/Values/BoundMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing.Ast;

Expand Down Expand Up @@ -45,7 +45,7 @@ public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] a

public override IPythonProjectEntry DeclaringModule => Function.DeclaringModule;
public override int DeclaringVersion => Function.DeclaringVersion;
public override IEnumerable<ILocationInfo> Locations => Function.Locations;
public override IEnumerable<ILocationInfo> Locations => Function.Locations.MaybeEnumerate();
public override BuiltinTypeId TypeId => BuiltinTypeId.Function;

public IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
Expand Down
10 changes: 1 addition & 9 deletions src/Analysis/Engine/Impl/Values/BuiltinNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,7 @@ public MemberContainerType ContainedValue {

public virtual ILocatedMember GetLocatedMember() => null;

public override IEnumerable<ILocationInfo> Locations {
get {
var locatedMem = GetLocatedMember();
if (locatedMem != null) {
return locatedMem.Locations;
}
return LocationInfo.Empty;
}
}
public override IEnumerable<ILocationInfo> Locations => GetLocatedMember()?.Locations.MaybeEnumerate();

public override bool Equals(object obj) {
if (obj is BuiltinNamespace<MemberContainerType> bn && GetType() == bn.GetType()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Analysis/Engine/Impl/Values/MemberReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.PythonTools.Analysis.Analyzer;
using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing.Ast;

Expand Down Expand Up @@ -62,7 +63,7 @@ public IEnumerable<IReferenceable> GetDefinitions(string name, IMemberContainer

var member = innerContainer.GetMember(context, name) as ILocatedMember;
if (member != null) {
res.AddRange(member.Locations.Select(loc => new DefinitionList(loc)));
res.AddRange(member.Locations.MaybeEnumerate().Select(loc => new DefinitionList(loc)));
}

return res;
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Values/MultipleMemberInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public override string ShortDescription {
}
}

public override IEnumerable<ILocationInfo> Locations => _members.SelectMany(m => m.Locations);
public override IEnumerable<ILocationInfo> Locations => _members.SelectMany(m => m.Locations.MaybeEnumerate());

IModule IModule.GetChildPackage(IModuleContext context, string name) {
var children = new List<AnalysisValue>();
Expand Down
148 changes: 68 additions & 80 deletions src/Analysis/Engine/Impl/Values/ProtocolInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@ public override PythonMemberType MemberType {
}


internal override void AddReference(Node node, AnalysisUnit analysisUnit) {
_references.GetReferences(analysisUnit.ProjectEntry as ProjectEntry)?.AddReference(new EncodedLocation(analysisUnit, node));
}
internal override void AddReference(Node node, AnalysisUnit analysisUnit)
=> _references.GetReferences(analysisUnit.ProjectEntry as ProjectEntry)?.AddReference(new EncodedLocation(analysisUnit, node));

public override IEnumerable<ILocationInfo> Locations {
get {
ReferenceList defns;
if (!_references.TryGetValue(DeclaringModule, out defns)) {
if (!_references.TryGetValue(DeclaringModule, out var defns)) {
return Enumerable.Empty<ILocationInfo>();
}
return defns.Definitions.Select(l => l.GetLocationInfo()).Where(l => l != null);
return defns.Definitions.Select(l => l.GetLocationInfo()).ExcludeDefault();
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we have two almost identical methods: ExcludeDefaults and WhereNotNull.

Copy link
Author

Choose a reason for hiding this comment

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

Yes ExcludeDefault came from RTVS extensions, looks like WhereNotNull is local thing

}
}

Expand All @@ -151,17 +149,14 @@ public override void AugmentAssign(AugmentedAssignStatement node, AnalysisUnit u
}
}

public override IAnalysisSet Await(Node node, AnalysisUnit unit) {
return AnalysisSet.UnionAll(_protocols.Select(p => p.Await(node, unit)));
}
public override IAnalysisSet Await(Node node, AnalysisUnit unit)
=> AnalysisSet.UnionAll(_protocols.Select(p => p.Await(node, unit)));

public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
return AnalysisSet.UnionAll(_protocols.Select(p => p.BinaryOperation(node, unit, operation, rhs)));
}
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs)
=> AnalysisSet.UnionAll(_protocols.Select(p => p.BinaryOperation(node, unit, operation, rhs)));

public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
return AnalysisSet.UnionAll(_protocols.Select(p => p.Call(node, unit, args, keywordArgNames)));
}
public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames)
=> AnalysisSet.UnionAll(_protocols.Select(p => p.Call(node, unit, args, keywordArgNames)));

public override void DeleteMember(Node node, AnalysisUnit unit, string name) {
foreach (var p in _protocols) {
Expand Down Expand Up @@ -273,85 +268,78 @@ public override bool Equals(object obj) {

public override int GetHashCode() => ObjectComparer.Instance.GetHashCode(_protocols);

internal override bool UnionEquals(AnalysisValue av, int strength) {
if (av is ProtocolInfo pi) {
if (strength > 0) {
return Name == pi.Name;
}
return ObjectComparer.Instance.Equals(_protocols, pi._protocols);
}
return false;
}
internal override bool UnionEquals(AnalysisValue av, int strength)
=> av is ProtocolInfo pi ? ObjectComparer.Instance.Equals(_protocols, pi._protocols) : false;

internal override int UnionHashCode(int strength) {
if (strength > 0) {
return Name.GetHashCode();
}
return GetHashCode();
internal override int UnionHashCode(int strength) {
if (strength > 0) {
return Name.GetHashCode();
}
return GetHashCode();
}

internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
if (strength > 0 && av is ProtocolInfo pi && pi.Push()) {
try {
var name = _protocols.OfType<NameProtocol>().FirstOrDefault();
if (_protocols.Count == 1 && name != null) {
return this;
}

var protocols = _protocols.Union(pi._protocols, out bool changed);
if (!changed) {
return this;
}
internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
if (strength > 0 && av is ProtocolInfo pi && pi.Push()) {
try {
var name = _protocols.OfType<NameProtocol>().FirstOrDefault();
if (_protocols.Count == 1 && name != null) {
return this;
}

if (name != null) {
protocols.Split<NameProtocol>(out _, out protocols);
protocols = protocols.Add(name);
}
var protocols = _protocols.Union(pi._protocols, out bool changed);
if (!changed) {
return this;
}

return new ProtocolInfo(DeclaringModule, State) {
_protocols = protocols
};
} finally {
pi.Pop();
if (name != null) {
protocols.Split<NameProtocol>(out _, out protocols);
protocols = protocols.Add(name);
}
}

return this;
return new ProtocolInfo(DeclaringModule, State) {
_protocols = protocols
};
} finally {
pi.Pop();
}
}

public virtual IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
var names = _protocols.OfType<NameProtocol>().ToArray();
Debug.Assert(names.Length <= 1, "Multiple names are not supported");
var name = names.FirstOrDefault();
if (name != null) {
return name.GetRichDescription();
}
return this;
}

var res = new List<KeyValuePair<string, string>>();
var namespaces = _protocols.OfType<NamespaceProtocol>().ToArray();
var tuples = _protocols.OfType<TupleProtocol>().ToArray();
var other = _protocols.OfType<Protocol>().Except(names).Except(namespaces).ToArray();

if (namespaces.Any()) {
var addComma = false;
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Misc, "("));
foreach (var p in namespaces) {
if (addComma) {
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Comma, ", "));
}
addComma = true;
res.AddRange(p.GetRichDescription());
public virtual IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
var names = _protocols.OfType<NameProtocol>().ToArray();
Debug.Assert(names.Length <= 1, "Multiple names are not supported");
var name = names.FirstOrDefault();
if (name != null) {
return name.GetRichDescription();
}

var res = new List<KeyValuePair<string, string>>();
var namespaces = _protocols.OfType<NamespaceProtocol>().ToArray();
var tuples = _protocols.OfType<TupleProtocol>().ToArray();
var other = _protocols.OfType<Protocol>().Except(names).Except(namespaces).ToArray();

if (namespaces.Any()) {
var addComma = false;
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Misc, "("));
foreach (var p in namespaces) {
if (addComma) {
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Comma, ", "));
}
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Misc, ")"));
addComma = true;
res.AddRange(p.GetRichDescription());
}
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Misc, ")"));
}

foreach (var p in other) {
res.AddRange(p.GetRichDescription().ToArray());
}
foreach (var p in other) {
res.AddRange(p.GetRichDescription().ToArray());
}

res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.EndOfDeclaration, string.Empty));
res.Add(new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.EndOfDeclaration, string.Empty));

return res;
}
return res;
}
}
}
Loading