Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Make self mapping more error-proof (#163)
Browse files Browse the repository at this point in the history
* Fix self mapping

* lint
  • Loading branch information
gavrilikhin-d authored May 14, 2024
1 parent f5e3abc commit 996b208
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
8 changes: 8 additions & 0 deletions src/hir/declarations/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use indexmap::IndexMap;

use crate::{
compilation::Module,
hir::SelfType,
named::Named,
syntax::{Identifier, Keyword, Ranged},
AddSourceLocation,
Expand All @@ -25,6 +26,13 @@ pub struct Trait {
inner: Arc<RwLock<TraitData>>,
}

impl Trait {
/// Get self type for this trait
pub fn self_type(&self) -> SelfType {
SelfType::for_trait(self.clone())
}
}

impl DataHolder for Trait {
type Data = TraitData;

Expand Down
12 changes: 8 additions & 4 deletions src/hir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::{
fmt::{Debug, Display},
};

use crate::{
mutability::Mutable, named::Named, syntax::Identifier,
AddSourceLocation,
};
use crate::{mutability::Mutable, named::Named, syntax::Identifier, AddSourceLocation};

use super::{Basename, BuiltinClass, Class, Generic, Member, Trait, TypeReference};
use derive_more::{Display, From, TryInto};
Expand Down Expand Up @@ -108,6 +105,13 @@ pub struct SelfType {
pub associated_trait: Trait,
}

impl SelfType {
/// Create new self type for trait
pub fn for_trait(associated_trait: Trait) -> Self {
Self { associated_trait }
}
}

impl PartialEq for SelfType {
fn eq(&self, other: &Self) -> bool {
self.associated_trait == other.associated_trait
Expand Down
7 changes: 2 additions & 5 deletions src/semantics/contexts/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use crate::{
compilation::Compiler,
hir::{Function, FunctionData, FunctionNamePart, ModuleData, SelfType, Type, Typed},
hir::{Function, FunctionData, FunctionNamePart, ModuleData, Type, Typed},
semantics::{AddDeclaration, ConvertibleTo, FindDeclaration, Implements},
};

Expand Down Expand Up @@ -64,10 +64,7 @@ pub trait Context: FindDeclaration + AddDeclaration + Display {
where
Self: Sized,
{
let self_ty: Type = SelfType {
associated_trait: trait_fn.tr.clone().unwrap(),
}
.into();
let self_ty: Type = trait_fn.tr.clone().unwrap().self_type().into();
let mut context = GenericContext::for_generics(vec![self_ty.clone()], self);
if let Some(concrete) = self_type_specialization.clone() {
context.map_generic(self_ty, concrete);
Expand Down
10 changes: 4 additions & 6 deletions src/semantics/contexts/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ impl<'p> GenericContext<'p> {
pub fn for_fn(f: &FunctionData, parent: &'p mut impl Context) -> Self {
let mut candidate_context = Self::for_generics(f.generic_types.clone(), parent);

if let Some(ty) = f
.parameters()
.map(|p| p.ty())
.find(|ty| matches!(ty, Type::SelfType(_)))
{
candidate_context.generic_parameters.push(ty);
if let Some(tr) = &f.tr {
candidate_context
.generic_parameters
.push(tr.self_type().into());
}

return candidate_context;
Expand Down
9 changes: 2 additions & 7 deletions src/semantics/contexts/trait.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Display;

use crate::{
hir::{Class, Function, SelfType, Trait, Type, Variable},
hir::{Class, Function, Trait, Type, Variable},
named::Named,
semantics::{AddDeclaration, FindDeclaration, FindDeclarationHere},
};
Expand Down Expand Up @@ -43,12 +43,7 @@ impl FindDeclarationHere for TraitContext<'_> {
return None;
}

Some(
SelfType {
associated_trait: self.tr.clone(),
}
.into(),
)
Some(self.tr.self_type().into())
}

fn functions_with_n_name_parts_here(&self, n: usize) -> Vec<Function> {
Expand Down

0 comments on commit 996b208

Please sign in to comment.