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

trait加法错误检查 #281

Open
RINNE-TAN opened this issue Apr 26, 2023 · 0 comments
Open

trait加法错误检查 #281

RINNE-TAN opened this issue Apr 26, 2023 · 0 comments
Assignees

Comments

@RINNE-TAN
Copy link
Member

目前的trait加法以及继承

trait A {
    fn a() bool;
}

trait B {
    fn b() i64;
}

trait C: A+B {
    fn c() void;
}

相当于C继承了A、B的方法变成

trait C {
    fn c() void;
    fn b() i64;
    fn a() bool;
}

但是在出现同名方法的时候(只需要名字一样,不需要整个函数签名一样),需要报错
比如以下几种情况都需要报错

trait A {
    fn a() bool;
}
trait B: A {
    fn a() void;
}
trait A {
    fn a() bool;
}

trait B {
    fn a() i64;
}

trait C: A+B {
    fn c() void;
}

需要关注的代码

pub struct STType {
    pub name: String,
    pub path: String,
    pub fields: LinkedHashMap<String, Field>,
    pub range: Range,
    pub doc: Vec<Box<NodeEnum>>,
    pub generic_map: IndexMap<String, Arc<RefCell<PLType>>>,
    pub derives: Vec<Arc<RefCell<PLType>>>,
    pub modifier: Option<(TokenType, Range)>,
    pub body_range: Range,
    pub is_trait: bool,
}

对于Trait C: A+B来说,derives就是[A,B]
需要在这个数组做初始化的时候,检查A,B,C的field是否存在名字冲突,初始化的逻辑在MultiTraitNode的get_types方法被调用的地方,需要注意的是A和B本身也可能继承别的trait,整个检查过程需要递归完成。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants