Skip to content

Commit 7eae498

Browse files
bors[bot]Marwes
andauthored
Merge #859
859: Improve program analysis for the language server r=Marwes a=Marwes Co-authored-by: Markus Westerlind <[email protected]>
2 parents c7ec4dc + 564decc commit 7eae498

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3562
-3008
lines changed

Cargo.lock

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

base/src/ast.rs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ use crate::{
2424
resolve::remove_aliases_cow,
2525
symbol::Symbol,
2626
types::{
27-
self, Alias, AliasData, ArcType, ArgType, Field, Generic, NullInterner, Type, TypeEnv,
28-
TypeExt, TypePtr,
27+
self, Alias, AliasData, ArcType, ArgType, AsId, Field, Generic, NullInterner, Type,
28+
TypeEnv, TypeExt, TypePtr,
2929
},
3030
};
3131

32+
pub type Sp<T> = Spanned<T, BytePos>;
33+
3234
pub trait DisplayEnv {
3335
type Ident;
3436

@@ -76,13 +78,13 @@ impl<'a, T: ?Sized + IdentEnv> IdentEnv for &'a mut T {
7678
}
7779
}
7880

79-
#[derive(Eq, PartialEq, Debug, AstClone)]
81+
#[derive(Eq, PartialEq, AstClone)]
8082
pub struct InnerAstType<'ast, Id> {
8183
metadata: BaseMetadata<'ast>,
8284
typ: Spanned<Type<Id, AstType<'ast, Id>>, BytePos>,
8385
}
8486

85-
#[derive(Eq, PartialEq, Debug, AstClone)]
87+
#[derive(Eq, PartialEq, AstClone)]
8688
pub struct AstType<'ast, Id> {
8789
_typ: &'ast mut InnerAstType<'ast, Id>,
8890
}
@@ -106,7 +108,16 @@ impl<'ast, Id> DerefMut for AstType<'ast, Id> {
106108
}
107109
}
108110

109-
impl<Id: AsRef<str>> fmt::Display for AstType<'_, Id> {
111+
impl<Id: fmt::Debug> fmt::Debug for AstType<'_, Id> {
112+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113+
f.debug_struct("AstType")
114+
.field("metadata", &self._typ.metadata)
115+
.field("typ", &self._typ.typ)
116+
.finish()
117+
}
118+
}
119+
120+
impl<Id: AsRef<str> + AsId<Id>> fmt::Display for AstType<'_, Id> {
110121
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111122
write!(f, "{}", types::TypeFormatter::new(self))
112123
}
@@ -120,10 +131,11 @@ impl<Id> HasSpan for AstType<'_, Id> {
120131

121132
impl<'ast, Id> TypePtr for AstType<'ast, Id> {
122133
type Id = Id;
134+
type SpannedId = Spanned<Id, BytePos>;
123135
type Types = &'ast mut [AstType<'ast, Id>];
124136
type Generics = &'ast mut [Generic<Id>];
125-
type Fields = &'ast mut [Field<Id, Self>];
126-
type TypeFields = &'ast mut [Field<Id, Alias<Id, Self>>];
137+
type Fields = &'ast mut [Field<Self::SpannedId, Self>];
138+
type TypeFields = &'ast mut [Field<Self::SpannedId, Alias<Id, Self>>];
127139
}
128140

129141
pub trait HasMetadata {
@@ -187,6 +199,10 @@ impl<'ast, Id> AstType<'ast, Id> {
187199
_ => self,
188200
}
189201
}
202+
203+
pub fn span_mut(&mut self) -> &mut Span<BytePos> {
204+
&mut self._typ.typ.span
205+
}
190206
}
191207

192208
impl<'ast, Id> AsMut<SpannedAstType<'ast, Id>> for AstType<'ast, Id> {
@@ -740,7 +756,7 @@ pub trait $trait_name<'a, 'ast> {
740756
}
741757
fn visit_spanned_ident(&mut self, _: &'a $($mut)* Spanned<Self::Ident, BytePos>) {}
742758
fn visit_typ(&mut self, _: &'a $($mut)* ArcType<Self::Ident>) {}
743-
fn visit_ast_type(&mut self, s: &'a $($mut)* SpannedAstType<'ast, Self::Ident>) {
759+
fn visit_ast_type(&mut self, s: &'a $($mut)* AstType<'ast, Self::Ident>) {
744760
walk_ast_type(self, s);
745761
}
746762
}
@@ -751,7 +767,7 @@ pub fn walk_alias<'a, 'ast, V>(
751767
)
752768
where V: ?Sized + $trait_name<'a, 'ast>,
753769
{
754-
v.visit_ast_type(&$($mut)* alias.value.$unresolved_type()._typ.typ);
770+
v.visit_ast_type(alias.value.$unresolved_type());
755771
}
756772

757773
pub fn walk_expr<'a, 'ast, V>(v: &mut V, e: &'a $($mut)* SpannedExpr<'ast, V::Ident>)
@@ -785,7 +801,7 @@ pub fn walk_expr<'a, 'ast, V>(v: &mut V, e: &'a $($mut)* SpannedExpr<'ast, V::Id
785801
v.visit_typ(&$($mut)* bind.resolved_type);
786802
v.visit_expr(&$($mut)* bind.expr);
787803
if let Some(ref $($mut)* ast_type) = bind.typ {
788-
v.visit_ast_type(&$($mut)* ast_type._typ.typ)
804+
v.visit_ast_type(ast_type)
789805
}
790806
}
791807
v.visit_expr(body);
@@ -946,50 +962,52 @@ pub fn walk_pattern<'a, 'ast,V: ?Sized + $trait_name<'a, 'ast>>(v: &mut V, p: &'
946962

947963
pub fn walk_ast_type<'a, 'ast, V: ?Sized + $trait_name<'a, 'ast>>(
948964
v: &mut V,
949-
s: &'a $($mut)* SpannedAstType<'ast, V::Ident>,
965+
s: &'a $($mut)* AstType<'ast, V::Ident>,
950966
) {
951-
match s.value {
967+
match **s {
952968
Type::Hole | Type::Opaque | Type::Error | Type::Builtin(_) => (),
953969
Type::Forall(_, ref $($mut)* ast_type) => {
954-
v.visit_ast_type(&$($mut)* ast_type._typ.typ);
970+
v.visit_ast_type(ast_type);
955971
}
956972
Type::Function(_, ref $($mut)* arg, ref $($mut)* ret) => {
957-
v.visit_ast_type(&$($mut)* arg._typ.typ);
958-
v.visit_ast_type(&$($mut)* ret._typ.typ);
973+
v.visit_ast_type(arg);
974+
v.visit_ast_type(ret);
959975
}
960976
Type::App(ref $($mut)* ast_type, ref $($mut)* ast_types) => {
961977
for ast_type in & $($mut)* **ast_types {
962-
v.visit_ast_type(&$($mut)* ast_type._typ.typ);
978+
v.visit_ast_type(ast_type);
963979
}
964-
v.visit_ast_type(&$($mut)* ast_type._typ.typ)
980+
v.visit_ast_type(ast_type)
965981
}
966-
Type::Record(ref $($mut)* ast_type) => v.visit_ast_type(&$($mut)* ast_type._typ.typ),
967-
Type::Variant(ref $($mut)* ast_type) => v.visit_ast_type(&$($mut)* ast_type._typ.typ),
968-
Type::Effect(ref $($mut)* ast_type) => v.visit_ast_type(&$($mut)* ast_type._typ.typ),
982+
Type::Record(ref $($mut)* ast_type) => v.visit_ast_type(ast_type),
983+
Type::Variant(ref $($mut)* ast_type) => v.visit_ast_type(ast_type),
984+
Type::Effect(ref $($mut)* ast_type) => v.visit_ast_type(ast_type),
969985
Type::EmptyRow => (),
970986
Type::ExtendRow {
971987
ref $($mut)* fields,
972988
ref $($mut)* rest,
973989
} => {
974990
for field in & $($mut)* **fields {
975-
v.visit_ast_type(&$($mut)* field.typ._typ.typ);
991+
v.visit_spanned_ident(& $($mut)* field.name);
992+
v.visit_ast_type(&$($mut)* field.typ);
976993
}
977-
v.visit_ast_type(&$($mut)* rest._typ.typ);
994+
v.visit_ast_type(rest);
978995
}
979996
Type::ExtendTypeRow {
980997
ref $($mut)* types,
981998
ref $($mut)* rest,
982999
} => {
9831000
for field in & $($mut)* **types {
1001+
v.visit_spanned_ident(& $($mut)* field.name);
9841002
if let Some(alias) = field.typ.$try_get_alias() {
985-
v.visit_ast_type(&$($mut)* alias.$unresolved_type()._typ.typ);
1003+
v.visit_ast_type(alias.$unresolved_type());
9861004
}
9871005
}
988-
v.visit_ast_type(&$($mut)* rest._typ.typ);
1006+
v.visit_ast_type(rest);
9891007
}
9901008
Type::Alias(ref $($mut)* alias) => {
9911009
if let Some(alias) = alias.$try_get_alias() {
992-
v.visit_ast_type(&$($mut)* alias.$unresolved_type()._typ.typ);
1010+
v.visit_ast_type(alias.$unresolved_type());
9931011
}
9941012
}
9951013
Type::Ident(_) | Type::Projection(_) | Type::Variable(_) | Type::Generic(_) | Type::Skolem(_) => (),
@@ -1282,8 +1300,8 @@ impl_ast_arena! {
12821300
InnerAstType<'ast, Id> => types,
12831301
AstType<'ast, Id> => type_ptrs,
12841302
Generic<Id> => generics,
1285-
Field<Id, AstType<'ast, Id>> => type_fields,
1286-
Field<Id, Alias<Id, AstType<'ast, Id>>> => type_type_fields,
1303+
Field<Spanned<Id, BytePos>, AstType<'ast, Id>> => type_fields,
1304+
Field<Spanned<Id, BytePos>, Alias<Id, AstType<'ast, Id>>> => type_type_fields,
12871305
Metadata => metadata,
12881306
}
12891307

base/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ impl<E: fmt::Display> InFile<E> {
218218
.name()
219219
}
220220

221+
pub fn source(&self) -> &crate::source::CodeMap {
222+
&self.source
223+
}
224+
221225
pub fn errors(&self) -> &Errors<Spanned<E, BytePos>> {
222226
&self.error
223227
}

base/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ macro_rules! type_cache {
7575

7676
#[macro_export]
7777
macro_rules! chain {
78-
($alloc: expr; $first: expr, $($rest: expr),+ $(,)?) => {{
78+
($alloc: expr, $first: expr, $($rest: expr),+ $(,)?) => {{
7979
let mut doc = ::pretty::DocBuilder($alloc, $first.into());
8080
$(
8181
doc = doc.append($rest);

base/src/pos.rs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@ pub struct Positioned<T, Pos> {
4949
}
5050

5151
/// A region of code in a source file
52-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd)]
52+
#[derive(Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd)]
5353
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
5454
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
5555
pub struct Span<I> {
5656
start: I,
5757
end: I,
5858
}
5959

60+
impl<I: fmt::Debug> fmt::Debug for Span<I> {
61+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62+
write!(f, "{:?}..{:?}", self.start, self.end)
63+
}
64+
}
65+
6066
impl<I: Ord> Span<I> {
6167
/// Create a new span
6268
///
@@ -176,6 +182,10 @@ impl<I: Index> Span<I> {
176182
self.start() <= other.start() && other.end() <= self.end()
177183
}
178184

185+
pub fn contains_pos(self, other: I) -> bool {
186+
self.start() <= other && other <= self.end()
187+
}
188+
179189
/// Return `Equal` if `self` contains `pos`, otherwise it returns `Less` if `pos` is before
180190
/// `start` or `Greater` if `pos` is after or at `end`.
181191
///
@@ -309,6 +319,50 @@ pub struct Spanned<T, Pos> {
309319
pub value: T,
310320
}
311321

322+
impl<T, Pos> From<T> for Spanned<T, Pos>
323+
where
324+
Pos: Default,
325+
{
326+
fn from(value: T) -> Self {
327+
Spanned {
328+
span: Span::default(),
329+
value,
330+
}
331+
}
332+
}
333+
334+
impl<T, Pos> PartialEq<T> for Spanned<T, Pos>
335+
where
336+
T: PartialEq,
337+
{
338+
fn eq(&self, other: &T) -> bool {
339+
self.value == *other
340+
}
341+
}
342+
343+
impl<T, Pos> std::ops::Deref for Spanned<T, Pos> {
344+
type Target = T;
345+
fn deref(&self) -> &T {
346+
&self.value
347+
}
348+
}
349+
350+
impl<T, Pos> std::ops::DerefMut for Spanned<T, Pos> {
351+
fn deref_mut(&mut self) -> &mut T {
352+
&mut self.value
353+
}
354+
}
355+
356+
impl<T, U, Pos> AsRef<U> for Spanned<T, Pos>
357+
where
358+
T: AsRef<U>,
359+
U: ?Sized,
360+
{
361+
fn as_ref(&self) -> &U {
362+
self.value.as_ref()
363+
}
364+
}
365+
312366
impl<T, Pos> std::hash::Hash for Spanned<T, Pos>
313367
where
314368
T: std::hash::Hash,
@@ -357,10 +411,7 @@ pub fn spanned2<T, Pos>(start: Pos, end: Pos, value: T) -> Spanned<T, Pos>
357411
where
358412
Pos: Ord,
359413
{
360-
Spanned {
361-
span: span(start, end),
362-
value,
363-
}
414+
spanned(span(start, end), value)
364415
}
365416

366417
pub trait HasSpan {

base/src/resolve.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<T> AliasRemover<T> {
6060

6161
impl<T> AliasRemover<T>
6262
where
63-
T: TypeExt<Id = Symbol> + Clone + ::std::fmt::Display,
63+
T: TypeExt<Id = Symbol, SpannedId = Symbol> + Clone + ::std::fmt::Display,
6464
T::Types: Clone + Default + Extend<T> + FromIterator<T>,
6565
T::Generics: Clone + FromIterator<Generic<Symbol>>,
6666
T::Fields: Clone,
@@ -243,7 +243,7 @@ pub fn remove_aliases<T>(
243243
mut typ: T,
244244
) -> T
245245
where
246-
T: TypeExt<Id = Symbol> + Clone + ::std::fmt::Display,
246+
T: TypeExt<Id = Symbol, SpannedId = Symbol> + Clone + ::std::fmt::Display,
247247
T::Types: Clone + Default + Extend<T> + FromIterator<T>,
248248
T::Generics: Clone + FromIterator<Generic<Symbol>>,
249249
T::Fields: Clone,
@@ -261,7 +261,7 @@ pub fn remove_aliases_cow<'t, T>(
261261
typ: &'t T,
262262
) -> Cow<'t, T>
263263
where
264-
T: TypeExt<Id = Symbol> + Clone + ::std::fmt::Display,
264+
T: TypeExt<Id = Symbol, SpannedId = Symbol> + Clone + ::std::fmt::Display,
265265
T::Types: Clone + Default + Extend<T> + FromIterator<T>,
266266
T::Generics: Clone + FromIterator<Generic<Symbol>>,
267267
T::Fields: Clone,
@@ -282,7 +282,7 @@ pub fn canonical_alias<'t, F, T>(
282282
) -> Cow<'t, T>
283283
where
284284
F: FnMut(&AliasRef<Symbol, T>) -> bool,
285-
T: TypeExt<Id = Symbol> + Clone + ::std::fmt::Display,
285+
T: TypeExt<Id = Symbol, SpannedId = Symbol> + Clone + ::std::fmt::Display,
286286
T::Types: Clone + Default + Extend<T> + FromIterator<T>,
287287
T::Generics: Clone + FromIterator<Generic<Symbol>>,
288288
T::Fields: Clone,
@@ -318,7 +318,7 @@ pub fn remove_alias<T>(
318318
typ: &T,
319319
) -> Result<Option<T>, Error>
320320
where
321-
T: TypeExt<Id = Symbol> + Clone + ::std::fmt::Display,
321+
T: TypeExt<Id = Symbol, SpannedId = Symbol> + Clone + ::std::fmt::Display,
322322
T::Types: Clone + Default + Extend<T> + FromIterator<T>,
323323
T::Generics: Clone + FromIterator<Generic<Symbol>>,
324324
T::Fields: Clone,
@@ -342,7 +342,7 @@ pub fn peek_alias<'t, T>(
342342
typ: &'t T,
343343
) -> Result<Option<AliasRef<Symbol, T>>, Error>
344344
where
345-
T: TypeExt<Id = Symbol> + Clone + ::std::fmt::Display,
345+
T: TypeExt<Id = Symbol, SpannedId = Symbol> + Clone + ::std::fmt::Display,
346346
T::Types: Clone + Default + Extend<T>,
347347
T::Generics: Clone + FromIterator<Generic<Symbol>>,
348348
T::Fields: Clone,

base/src/source.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Module containing types and functions for mapping between byte indexes and line and column
22
//! locations
3-
use std::{ops::Range, sync::Arc};
3+
use std::{fmt, ops::Range, sync::Arc};
44

55
use itertools::Itertools;
66

@@ -10,12 +10,20 @@ use codespan_reporting::files::{Files, SimpleFile};
1010

1111
pub type FileId = BytePos;
1212

13-
#[derive(Debug)]
1413
pub struct FileMap {
1514
file: SimpleFile<String, String>,
1615
span_start: FileId,
1716
}
1817

18+
impl fmt::Debug for FileMap {
19+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20+
f.debug_struct("FileMap")
21+
.field("file", &self.file.source())
22+
.field("span", &self.span())
23+
.finish()
24+
}
25+
}
26+
1927
impl FileMap {
2028
pub fn new(name: String, source: String) -> Self {
2129
Self {

0 commit comments

Comments
 (0)