Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial implementation of goto types and separating annotations #2914

Merged
merged 11 commits into from
Apr 26, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@
removed if it would otherwise be left blank.
([Milco Kats](https://github.com/katsmil))

- Hover for type annotations is now separate from the thing being annotated ([Ameen Radwan](https://github.com/Acepie))

- Go to definition now works for direct type annotations ([Ameen Radwan](https://github.com/Acepie))

### Bug Fixes

- Fixed [RUSTSEC-2021-0145](https://rustsec.org/advisories/RUSTSEC-2021-0145) by
Expand Down
53 changes: 52 additions & 1 deletion compiler-core/generated/schema_capnp.rs

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

2 changes: 2 additions & 0 deletions compiler-core/schema.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct TypeConstructor {
module @2 :Text;
publicity @3 :Publicity;
deprecated @4 :Text;
origin @5 :SrcSpan;
documentation @6 :Text;
}

struct AccessorsMap {
Expand Down
5 changes: 4 additions & 1 deletion compiler-core/src/analyse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ fn register_type_alias(
type_ast: resolved_type,
deprecation,
type_: _,
documentation: _,
documentation,
} = t;

// A type alias must not have the same name as any other type in the module.
Expand All @@ -392,6 +392,7 @@ fn register_type_alias(
typ,
deprecation: deprecation.clone(),
publicity: *publicity,
documentation: documentation.clone(),
},
)?;

Expand Down Expand Up @@ -425,6 +426,7 @@ fn register_types_from_custom_type<'a>(
deprecation,
opaque,
constructors,
documentation,
..
} = t;
assert_unique_type_name(names, name, *location)?;
Expand Down Expand Up @@ -461,6 +463,7 @@ fn register_types_from_custom_type<'a>(
parameters,
publicity,
typ,
documentation: documentation.clone(),
},
)?;

Expand Down
124 changes: 123 additions & 1 deletion compiler-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,63 @@ impl TypeAst {
},
}
}

pub fn find_node(&self, byte_index: u32, type_: Arc<Type>) -> Option<Located<'_>> {
if !self.location().contains(byte_index) {
return None;
}

match self {
TypeAst::Fn(TypeAstFn {
arguments, return_, ..
}) => type_
.fn_types()
.and_then(|(arg_types, ret_type)| {
if let Some(arg) = arguments
.iter()
.zip(arg_types)
.find_map(|(arg, arg_type)| arg.find_node(byte_index, arg_type.clone()))
{
return Some(arg);
}
if let Some(ret) = return_.find_node(byte_index, ret_type) {
return Some(ret);
}

None
})
.or(Some(Located::Annotation(self.location(), type_))),
TypeAst::Constructor(TypeAstConstructor { arguments, .. }) => type_
.constructor_types()
.and_then(|arg_types| {
if let Some(arg) = arguments
.iter()
.zip(arg_types)
.find_map(|(arg, arg_type)| arg.find_node(byte_index, arg_type.clone()))
{
return Some(arg);
}

None
})
.or(Some(Located::Annotation(self.location(), type_))),
TypeAst::Tuple(TypeAstTuple { elems, .. }) => type_
.tuple_types()
.and_then(|elem_types| {
if let Some(e) = elems
.iter()
.zip(elem_types)
.find_map(|(e, e_type)| e.find_node(byte_index, e_type.clone()))
{
return Some(e);
}

None
})
.or(Some(Located::Annotation(self.location(), type_))),
TypeAst::Var(_) | TypeAst::Hole(_) => Some(Located::Annotation(self.location(), type_)),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -589,6 +646,12 @@ impl TypedDefinition {
.iter()
.find(|arg| arg.location.contains(byte_index))
{
// Check if location is within the arg annotation.
if let Some(a) = &found_arg.annotation {
return a
.find_node(byte_index, found_arg.type_.clone())
.or(Some(Located::Arg(found_arg)));
}
return Some(Located::Arg(found_arg));
};

Expand All @@ -600,6 +663,15 @@ impl TypedDefinition {
return Some(Located::Statement(found_statement));
};

// Check if location is within the return annotation.
if let Some(l) = function
.return_annotation
.iter()
.find_map(|a| a.find_node(byte_index, function.return_type.clone()))
{
return Some(l);
};

// Note that the fn `.location` covers the function head, not
// the entire statement.
if function.location.contains(byte_index) {
Expand All @@ -612,6 +684,23 @@ impl TypedDefinition {
}

Definition::CustomType(custom) => {
// Check if location is within the type of one of the arguments of a constructor.
if let Some(annotation) = custom
.constructors
.iter()
.find(|constructor| constructor.location.contains(byte_index))
.and_then(|constructor| {
constructor
.arguments
.iter()
.find(|arg| arg.location.contains(byte_index))
})
.filter(|arg| arg.location.contains(byte_index))
.and_then(|arg| arg.ast.find_node(byte_index, arg.type_.clone()))
{
return Some(annotation);
}

// Note that the custom type `.location` covers the function
// head, not the entire statement.
if custom.full_location().contains(byte_index) {
Expand All @@ -621,7 +710,35 @@ impl TypedDefinition {
}
}

Definition::TypeAlias(_) | Definition::Import(_) | Definition::ModuleConstant(_) => {
Definition::TypeAlias(alias) => {
// Check if location is within the type being aliased.
if let Some(l) = alias.type_ast.find_node(byte_index, alias.type_.clone()) {
return Some(l);
}

if alias.location.contains(byte_index) {
Some(Located::ModuleStatement(self))
} else {
None
}
}

Definition::ModuleConstant(constant) => {
// Check if location is within the annotation.
if let Some(annotation) = &constant.annotation {
if let Some(l) = annotation.find_node(byte_index, constant.type_.clone()) {
return Some(l);
}
}

if constant.location.contains(byte_index) {
Some(Located::ModuleStatement(self))
} else {
None
}
}

Definition::Import(_) => {
if self.location().contains(byte_index) {
Some(Located::ModuleStatement(self))
} else {
Expand Down Expand Up @@ -1788,6 +1905,11 @@ pub type UntypedAssignment = Assignment<(), UntypedExpr>;

impl TypedAssignment {
pub fn find_node(&self, byte_index: u32) -> Option<Located<'_>> {
if let Some(annotation) = &self.annotation {
if let Some(l) = annotation.find_node(byte_index, self.pattern.type_()) {
return Some(l);
}
}
self.pattern
.find_node(byte_index)
.or_else(|| self.value.find_node(byte_index))
Expand Down
35 changes: 33 additions & 2 deletions compiler-core/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use self::project_compiler::{Built, Options, ProjectCompiler};
pub use self::telemetry::{NullTelemetry, Telemetry};

use crate::ast::{
CustomType, DefinitionLocation, TypedArg, TypedDefinition, TypedExpr, TypedFunction,
CustomType, DefinitionLocation, TypeAst, TypedArg, TypedDefinition, TypedExpr, TypedFunction,
TypedPattern, TypedStatement,
};
use crate::{
Expand Down Expand Up @@ -296,10 +296,26 @@ pub enum Located<'a> {
ModuleStatement(&'a TypedDefinition),
FunctionBody(&'a TypedFunction),
Arg(&'a TypedArg),
Annotation(SrcSpan, std::sync::Arc<type_::Type>),
}

impl<'a> Located<'a> {
pub fn definition_location(&self) -> Option<DefinitionLocation<'_>> {
// Looks up the type constructor for the given type and then create the location.
fn type_location(
&self,
importable_modules: &'a im::HashMap<EcoString, type_::ModuleInterface>,
type_: std::sync::Arc<type_::Type>,
) -> Option<DefinitionLocation<'_>> {
type_constructor_from_modules(importable_modules, type_).map(|t| DefinitionLocation {
module: Some(&t.module),
span: t.origin,
})
}

pub fn definition_location(
&self,
importable_modules: &'a im::HashMap<EcoString, type_::ModuleInterface>,
) -> Option<DefinitionLocation<'_>> {
match self {
Self::Pattern(pattern) => pattern.definition_location(),
Self::Statement(statement) => statement.definition_location(),
Expand All @@ -310,10 +326,25 @@ impl<'a> Located<'a> {
span: statement.location(),
}),
Self::Arg(_) => None,
Self::Annotation(_, type_) => self.type_location(importable_modules, type_.clone()),
}
}
}

// Looks up the type constructor for the given type
pub fn type_constructor_from_modules(
importable_modules: &im::HashMap<EcoString, type_::ModuleInterface>,
type_: std::sync::Arc<type_::Type>,
) -> Option<&type_::TypeConstructor> {
let type_ = type_::collapse_links(type_);
match type_.as_ref() {
type_::Type::Named { name, module, .. } => importable_modules
.get(module)
.and_then(|i| i.types.get(name)),
_ => None,
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Origin {
Src,
Expand Down
12 changes: 12 additions & 0 deletions compiler-core/src/language_server/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ where
// Store the compiled dependency module information
for module in &compiled_dependencies {
let path = module.input_path.as_os_str().to_string_lossy().to_string();
// strip canonicalised windows prefix
#[cfg(target_family = "windows")]
let path = path
.strip_prefix(r"\\?\")
.map(|s| s.to_string())
.unwrap_or(path);
let line_numbers = LineNumbers::new(&module.code);
let source = ModuleSourceInformation { path, line_numbers };
_ = self.sources.insert(module.name.clone(), source);
Expand All @@ -124,6 +130,12 @@ where
}
// Create the source information
let path = module.src_path.to_string();
// strip canonicalised windows prefix
#[cfg(target_family = "windows")]
Acepie marked this conversation as resolved.
Show resolved Hide resolved
let path = path
.strip_prefix(r"\\?\")
.map(|s| s.to_string())
.unwrap_or(path);
let line_numbers = module.line_numbers.clone();
let source = ModuleSourceInformation { path, line_numbers };
_ = self.sources.insert(name.clone(), source);
Expand Down
Loading
Loading