Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 0fc3302

Browse files
author
Mikhail Arkhipov
authored
Merge pull request microsoft#418 from MikhailArkhipov/master
Bug fixes
2 parents f86b466 + d2318b6 commit 0fc3302

18 files changed

+73
-138
lines changed

src/Analysis/Engine/Impl/EncodedLocation.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,9 @@ public EncodedLocation(ILocationResolver resolver, object location) {
5555

5656
public bool IsAlive => (Resolver as ICanExpire)?.IsAlive ?? true;
5757

58-
public override int GetHashCode() {
59-
return (Resolver?.GetHashCode() ?? 0) ^ (Location?.GetHashCode() ?? 0);
60-
}
58+
public override int GetHashCode() => (Resolver?.GetHashCode() ?? 0) ^ (Location?.GetHashCode() ?? 0);
6159

62-
public override bool Equals(object obj) {
63-
return obj is EncodedLocation location && Equals(location);
64-
}
60+
public override bool Equals(object obj) => obj is EncodedLocation location && Equals(location);
6561

6662
#region IEquatable<EncodedLocation> Members
6763

@@ -71,11 +67,6 @@ public bool Equals(EncodedLocation other) {
7167

7268
#endregion
7369

74-
public ILocationInfo GetLocationInfo() {
75-
if (Resolver == null) {
76-
return Location as ILocationInfo;
77-
}
78-
return Resolver.ResolveLocation(Location);
79-
}
70+
public ILocationInfo GetLocationInfo() => Resolver?.ResolveLocation(Location) ?? Location as ILocationInfo;
8071
}
8172
}

src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonBoundMethod.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System.Collections.Generic;
1818
using Microsoft.PythonTools.Analysis;
19+
using Microsoft.PythonTools.Analysis.Infrastructure;
1920

2021
namespace Microsoft.PythonTools.Interpreter.Ast {
2122
class AstPythonBoundMethod : IPythonBoundFunction, ILocatedMember {
@@ -27,6 +28,6 @@ public AstPythonBoundMethod(IPythonFunction function, IPythonType selfType) {
2728
public IPythonFunction Function { get; }
2829
public IPythonType SelfType { get; }
2930
public PythonMemberType MemberType => PythonMemberType.Method;
30-
public IEnumerable<ILocationInfo> Locations => (Function as ILocatedMember)?.Locations;
31+
public IEnumerable<ILocationInfo> Locations => (Function as ILocatedMember)?.Locations.MaybeEnumerate();
3132
}
3233
}

src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public string Documentation {
9494
public Uri DocumentUri { get; }
9595
public PythonMemberType MemberType => PythonMemberType.Module;
9696
public Dictionary<object, object> Properties { get; } = new Dictionary<object, object>();
97-
public IEnumerable<ILocationInfo> Locations { get; }
97+
public IEnumerable<ILocationInfo> Locations { get; } = Enumerable.Empty<ILocationInfo>();
9898

9999
public int AnalysisVersion => 1;
100100
public IModuleContext AnalysisContext => null;

src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public AstPythonProperty(
1414
ILocationInfo location
1515
) {
1616
IsReadOnly = true;
17-
Locations = new[] { location };
17+
Locations = location != null ? new[] { location } : Enumerable.Empty<ILocationInfo>();
1818
FunctionDefinition = definition;
1919
}
2020

src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private AstPythonType(string name, Dictionary<string, IMember> members, IEnumera
5858
_members = members;
5959
_mro = Array.Empty<IPythonType>();
6060
DeclaringModule = NoDeclModule;
61-
Locations = locations;
61+
Locations = locations ?? Enumerable.Empty<ILocationInfo>();
6262
}
6363

6464
internal void AddMembers(IEnumerable<KeyValuePair<string, IMember>> members, bool overwrite) {

src/Analysis/Engine/Impl/Interpreter/Ast/AstScrapedPythonModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public virtual IEnumerable<string> GetMemberNames(IModuleContext moduleContext)
9090
internal PythonAst Ast { get; private set; }
9191

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

9696
protected virtual List<string> GetScrapeArguments(IPythonInterpreterFactory factory) {

src/Analysis/Engine/Impl/MemberResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal MemberResult(string name, Func<IEnumerable<AnalysisValue>> vars, Func<P
8585
///
8686
/// New in 1.5.
8787
/// </summary>
88-
public IEnumerable<ILocationInfo> Locations => Values.SelectMany(ns => ns.Locations);
88+
public IEnumerable<ILocationInfo> Locations => Values.SelectMany(ns => ns.Locations.MaybeEnumerate()).ExcludeDefault();
8989

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

src/Analysis/Engine/Impl/ModuleAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ internal static IEnumerable<AnalysisVariable> ToVariables(IReferenceable referen
124124
varType = VariableType.Definition;
125125
}
126126

127-
foreach (var loc in type.Locations.WhereNotNull()) {
127+
foreach (var loc in type.Locations.MaybeEnumerate().WhereNotNull()) {
128128
yield return new AnalysisVariable(def, varType, loc);
129129
}
130130
}

src/Analysis/Engine/Impl/ModuleTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ internal override bool ModuleContainsMember(IModuleContext context, string name)
416416
return false;
417417
}
418418

419-
foreach (var location in type.Locations) {
419+
foreach (var location in type.Locations.MaybeEnumerate().WhereNotNull()) {
420420
if (location.FilePath != modInfo.ProjectEntry.FilePath) {
421421
// declared in another module
422422
return false;

src/Analysis/Engine/Impl/Values/BoundMethodInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
1010
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
1111
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12-
// MERCHANTABLITY OR NON-INFRINGEMENT.
12+
// MERCHANTABILITY OR NON-INFRINGEMENT.
1313
//
1414
// See the Apache Version 2.0 License for specific language governing
1515
// permissions and limitations under the License.
1616

1717
using System.Collections.Generic;
1818
using System.Linq;
19-
using System.Text;
19+
using Microsoft.PythonTools.Analysis.Infrastructure;
2020
using Microsoft.PythonTools.Interpreter;
2121
using Microsoft.PythonTools.Parsing.Ast;
2222

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

4646
public override IPythonProjectEntry DeclaringModule => Function.DeclaringModule;
4747
public override int DeclaringVersion => Function.DeclaringVersion;
48-
public override IEnumerable<ILocationInfo> Locations => Function.Locations;
48+
public override IEnumerable<ILocationInfo> Locations => Function.Locations.MaybeEnumerate();
4949
public override BuiltinTypeId TypeId => BuiltinTypeId.Function;
5050

5151
public IEnumerable<KeyValuePair<string, string>> GetRichDescription() {

src/Analysis/Engine/Impl/Values/BuiltinNamespace.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,7 @@ public MemberContainerType ContainedValue {
111111

112112
public virtual ILocatedMember GetLocatedMember() => null;
113113

114-
public override IEnumerable<ILocationInfo> Locations {
115-
get {
116-
var locatedMem = GetLocatedMember();
117-
if (locatedMem != null) {
118-
return locatedMem.Locations;
119-
}
120-
return LocationInfo.Empty;
121-
}
122-
}
114+
public override IEnumerable<ILocationInfo> Locations => GetLocatedMember()?.Locations.MaybeEnumerate();
123115

124116
public override bool Equals(object obj) {
125117
if (obj is BuiltinNamespace<MemberContainerType> bn && GetType() == bn.GetType()) {

src/Analysis/Engine/Impl/Values/MemberReferences.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using Microsoft.PythonTools.Analysis.Analyzer;
20+
using Microsoft.PythonTools.Analysis.Infrastructure;
2021
using Microsoft.PythonTools.Interpreter;
2122
using Microsoft.PythonTools.Parsing.Ast;
2223

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

6364
var member = innerContainer.GetMember(context, name) as ILocatedMember;
6465
if (member != null) {
65-
res.AddRange(member.Locations.Select(loc => new DefinitionList(loc)));
66+
res.AddRange(member.Locations.MaybeEnumerate().Select(loc => new DefinitionList(loc)));
6667
}
6768

6869
return res;

src/Analysis/Engine/Impl/Values/MultipleMemberInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public override string ShortDescription {
228228
}
229229
}
230230

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

233233
IModule IModule.GetChildPackage(IModuleContext context, string name) {
234234
var children = new List<AnalysisValue>();

src/Analysis/Engine/Impl/Values/ProtocolInfo.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,15 @@ public override PythonMemberType MemberType {
129129
}
130130

131131

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

136135
public override IEnumerable<ILocationInfo> Locations {
137136
get {
138-
ReferenceList defns;
139-
if (!_references.TryGetValue(DeclaringModule, out defns)) {
137+
if (!_references.TryGetValue(DeclaringModule, out var defns)) {
140138
return Enumerable.Empty<ILocationInfo>();
141139
}
142-
return defns.Definitions.Select(l => l.GetLocationInfo()).Where(l => l != null);
140+
return defns.Definitions.Select(l => l.GetLocationInfo()).ExcludeDefault();
143141
}
144142
}
145143

@@ -151,17 +149,14 @@ public override void AugmentAssign(AugmentedAssignStatement node, AnalysisUnit u
151149
}
152150
}
153151

154-
public override IAnalysisSet Await(Node node, AnalysisUnit unit) {
155-
return AnalysisSet.UnionAll(_protocols.Select(p => p.Await(node, unit)));
156-
}
152+
public override IAnalysisSet Await(Node node, AnalysisUnit unit)
153+
=> AnalysisSet.UnionAll(_protocols.Select(p => p.Await(node, unit)));
157154

158-
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
159-
return AnalysisSet.UnionAll(_protocols.Select(p => p.BinaryOperation(node, unit, operation, rhs)));
160-
}
155+
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs)
156+
=> AnalysisSet.UnionAll(_protocols.Select(p => p.BinaryOperation(node, unit, operation, rhs)));
161157

162-
public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
163-
return AnalysisSet.UnionAll(_protocols.Select(p => p.Call(node, unit, args, keywordArgNames)));
164-
}
158+
public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames)
159+
=> AnalysisSet.UnionAll(_protocols.Select(p => p.Call(node, unit, args, keywordArgNames)));
165160

166161
public override void DeleteMember(Node node, AnalysisUnit unit, string name) {
167162
foreach (var p in _protocols) {
@@ -273,22 +268,10 @@ public override bool Equals(object obj) {
273268

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

276-
internal override bool UnionEquals(AnalysisValue av, int strength) {
277-
if (av is ProtocolInfo pi) {
278-
if (strength > 0) {
279-
return Name == pi.Name;
280-
}
281-
return ObjectComparer.Instance.Equals(_protocols, pi._protocols);
282-
}
283-
return false;
284-
}
271+
internal override bool UnionEquals(AnalysisValue av, int strength)
272+
=> av is ProtocolInfo pi && ObjectComparer.Instance.Equals(_protocols, pi._protocols);
285273

286-
internal override int UnionHashCode(int strength) {
287-
if (strength > 0) {
288-
return Name.GetHashCode();
289-
}
290-
return GetHashCode();
291-
}
274+
internal override int UnionHashCode(int strength) => strength > 0 ? Name.GetHashCode() : GetHashCode();
292275

293276
internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
294277
if (strength > 0 && av is ProtocolInfo pi && pi.Push()) {

src/Analysis/Engine/Impl/Values/Protocols.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,19 @@ protected IAnalysisSet MakeMethod(string qualname, IReadOnlyList<IAnalysisSet> a
6363

6464
public override PythonMemberType MemberType => PythonMemberType.Unknown;
6565

66-
// Do not return any default values from protocols. We call these directly and handle null.
67-
public override IAnalysisSet Await(Node node, AnalysisUnit unit) => null;
68-
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) => null;
69-
public override IAnalysisSet GetAsyncEnumeratorTypes(Node node, AnalysisUnit unit) => null;
70-
public override IAnalysisSet GetAsyncIterator(Node node, AnalysisUnit unit) => null;
71-
public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit) => null;
72-
public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, AnalysisValue instance, AnalysisValue context) => null;
73-
public override IAnalysisSet GetEnumeratorTypes(Node node, AnalysisUnit unit) => null;
74-
public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) => null;
75-
public override IAnalysisSet GetInstanceType() => null;
76-
public override IEnumerable<KeyValuePair<IAnalysisSet, IAnalysisSet>> GetItems() => null;
77-
public override IAnalysisSet GetIterator(Node node, AnalysisUnit unit) => null;
78-
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => null;
79-
public override IAnalysisSet UnaryOperation(Node node, AnalysisUnit unit, PythonOperator operation) => null;
66+
public override IAnalysisSet Await(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
67+
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) => AnalysisSet.Empty;
68+
public override IAnalysisSet GetAsyncEnumeratorTypes(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
69+
public override IAnalysisSet GetAsyncIterator(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
70+
public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit) => AnalysisSet.Empty;
71+
public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, AnalysisValue instance, AnalysisValue context) => AnalysisSet.Empty;
72+
public override IAnalysisSet GetEnumeratorTypes(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
73+
public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) => AnalysisSet.Empty;
74+
public override IAnalysisSet GetInstanceType() => AnalysisSet.Empty;
75+
public override IEnumerable<KeyValuePair<IAnalysisSet, IAnalysisSet>> GetItems() => Enumerable.Empty< KeyValuePair<IAnalysisSet, IAnalysisSet>>();
76+
public override IAnalysisSet GetIterator(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
77+
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
78+
public override IAnalysisSet UnaryOperation(Node node, AnalysisUnit unit, PythonOperator operation) => AnalysisSet.Empty;
8079

8180
public override IDictionary<string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None) {
8281
EnsureMembers();
@@ -131,21 +130,22 @@ class NameProtocol : Protocol {
131130
private readonly BuiltinTypeId _typeId;
132131
private List<KeyValuePair<string, string>> _richDescription;
133132

134-
public NameProtocol(ProtocolInfo self, string name, string documentation = null, BuiltinTypeId typeId = BuiltinTypeId.Unknown, PythonMemberType memberType = PythonMemberType.Unknown) : base(self) {
133+
public NameProtocol(
134+
ProtocolInfo self,
135+
string name,
136+
string documentation = null,
137+
BuiltinTypeId typeId = BuiltinTypeId.Unknown,
138+
PythonMemberType memberType = PythonMemberType.Unknown)
139+
: base(self) {
135140
_name = name;
136141
_doc = documentation;
137142
_typeId = typeId;
138143
MemberType = memberType;
139144
_richDescription = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Type, _name) };
140145
}
141146

142-
public NameProtocol(ProtocolInfo self, IPythonType type) : base(self) {
143-
_name = type.Name;
144-
_doc = type.Documentation;
145-
_typeId = type.TypeId;
146-
MemberType = type.MemberType;
147-
_richDescription = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Type, _name) };
148-
}
147+
public NameProtocol(ProtocolInfo self, IPythonType type)
148+
: this(self, type.Name, type.Documentation, type.TypeId, type.MemberType) { }
149149

150150
public void ExtendDescription(KeyValuePair<string, string> part) {
151151
_richDescription.Add(part);
@@ -851,12 +851,12 @@ public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, Analysis
851851
public override IEnumerable<KeyValuePair<IAnalysisSet, IAnalysisSet>> GetItems() => _actualType.GetItems();
852852
public override IAnalysisSet GetIterator(Node node, AnalysisUnit unit) => _actualType.GetIterator(node, unit);
853853
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => _actualType.GetReturnForYieldFrom(node, unit);
854-
public override IEnumerable<ILocationInfo> Locations => _actualType.Locations;
854+
public override IEnumerable<ILocationInfo> Locations => _actualType.Locations.MaybeEnumerate();
855855
public override IMro Mro => _actualType.Mro;
856-
public override IEnumerable<OverloadResult> Overloads => _actualType.Overloads;
856+
public override IEnumerable<OverloadResult> Overloads => _actualType.Overloads.MaybeEnumerate();
857857
public override int? GetLength() => _actualType.GetLength();
858858
internal override IAnalysisSet Resolve(AnalysisUnit unit, ResolutionContext context) => _actualType.Resolve(unit, context);
859-
internal override IEnumerable<ILocationInfo> References => _actualType.References;
859+
internal override IEnumerable<ILocationInfo> References => _actualType.References.MaybeEnumerate();
860860
public override IAnalysisSet ReverseBinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs)
861861
=> _actualType.ReverseBinaryOperation(node, unit, operation, rhs);
862862

0 commit comments

Comments
 (0)