Skip to content

Commit 1739aba

Browse files
authored
Merge pull request #318 from Pivot-Studio/Chronostasys/issue317
feat: trait type generic
2 parents f3d243b + 4e1e2eb commit 1739aba

28 files changed

+1455
-351
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ jobs:
142142
args: nextest --workspace --all-features --lcov --output-path lcov.info --profile ci -vv
143143

144144
- name: Upload coverage to Codecov
145-
uses: codecov/codecov-action@v3
145+
uses: codecov/codecov-action@v3.1.4
146146
with:
147147
token: ${{ secrets.CODE_COV }}
148148
files: lcov.info
149149
name: ${{ matrix.os }}
150-
fail_ci_if_error: true
150+
fail_ci_if_error: false
151151
verbose: true
152152
style:
153153
name: Check Style

planglib/std/__private.pi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
use std::io;
2-
use std::stdbuiltin;
2+
use std::stdbuiltin;
3+
use std::iter;

planglib/std/iter.pi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use core::builtin::*;
2+
3+
pub trait Iterator<T> {
4+
fn next() Option<T>;
5+
}
6+
7+
struct name<T> {
8+
t:T;
9+
}
10+
11+
impl <T> Iterator<T> for name<T> {
12+
fn next() Option<T> {
13+
return self.t;
14+
}
15+
}
16+

src/ast/ctx.rs

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -372,55 +372,60 @@ impl<'a, 'ctx> Ctx<'a> {
372372
if let (PLType::Trait(t), PLType::Struct(st)) =
373373
(&*target_pltype.borrow(), &*st_pltype.borrow())
374374
{
375-
if !st.implements_trait(t, &self.get_root_ctx().plmod) {
376-
return Err(mismatch_err!(
377-
self,
378-
ori_range,
379-
target_range,
380-
target_pltype.borrow(),
381-
st_pltype.borrow()
382-
));
383-
}
384-
let trait_handle = builder.alloc("tmp_traitv", &target_pltype.borrow(), self, None);
385-
for f in t.list_trait_fields().iter() {
386-
let mthd = find_mthd(st, f, t).unwrap_or_else(|| {
387-
for t in &t.derives {
388-
match &*t.borrow() {
389-
PLType::Trait(t) => {
390-
if let Some(mthd) = find_mthd(st, f, t) {
391-
return mthd;
375+
return self.run_in_type_mod(t, |ctx, t| {
376+
ctx.protect_generic_context(&t.generic_map, |ctx| {
377+
if !st.implements_trait(t, &ctx.get_root_ctx().plmod) {
378+
return Err(mismatch_err!(
379+
ctx,
380+
ori_range,
381+
target_range,
382+
target_pltype.borrow(),
383+
st_pltype.borrow()
384+
));
385+
}
386+
let trait_handle =
387+
builder.alloc("tmp_traitv", &target_pltype.borrow(), ctx, None);
388+
for f in t.list_trait_fields().iter() {
389+
let mthd = find_mthd(st, f, t).unwrap_or_else(|| {
390+
for t in &t.derives {
391+
match &*t.borrow() {
392+
PLType::Trait(t) => {
393+
if let Some(mthd) = find_mthd(st, f, t) {
394+
return mthd;
395+
}
396+
}
397+
_ => unreachable!(),
392398
}
393399
}
394-
_ => unreachable!(),
395-
}
396-
}
397-
unreachable!()
398-
});
400+
unreachable!()
401+
});
399402

400-
// TODO: let a:trait = B<i64>{} panic
401-
let fnhandle = builder.get_or_insert_fn_handle(&mthd.borrow(), self);
402-
let targetftp = f.typenode.get_type(self, builder, true).unwrap();
403-
let casted = builder.bitcast(self, fnhandle, &targetftp.borrow(), "fncast_tmp");
404-
let f_ptr = builder
405-
.build_struct_gep(trait_handle, f.index, "field_tmp")
406-
.unwrap();
407-
builder.build_store(f_ptr, casted);
408-
}
409-
let st_value = builder.bitcast(
410-
self,
411-
st_value,
412-
&PLType::Pointer(Arc::new(RefCell::new(PLType::Primitive(PriType::I64)))),
413-
"traitcast_tmp",
414-
);
415-
let v_ptr = builder.build_struct_gep(trait_handle, 1, "v_tmp").unwrap();
416-
builder.build_store(v_ptr, st_value);
417-
let type_hash = builder
418-
.build_struct_gep(trait_handle, 0, "tp_hash")
419-
.unwrap();
420-
let hash = st.get_type_code();
421-
let hash = builder.int_value(&PriType::U64, hash, false);
422-
builder.build_store(type_hash, hash);
423-
return Ok(trait_handle);
403+
let fnhandle = builder.get_or_insert_fn_handle(&mthd.borrow(), ctx);
404+
let targetftp = f.typenode.get_type(ctx, builder, true).unwrap();
405+
let casted =
406+
builder.bitcast(ctx, fnhandle, &targetftp.borrow(), "fncast_tmp");
407+
let f_ptr = builder
408+
.build_struct_gep(trait_handle, f.index, "field_tmp")
409+
.unwrap();
410+
builder.build_store(f_ptr, casted);
411+
}
412+
let st_value = builder.bitcast(
413+
ctx,
414+
st_value,
415+
&PLType::Pointer(Arc::new(RefCell::new(PLType::Primitive(PriType::I64)))),
416+
"traitcast_tmp",
417+
);
418+
let v_ptr = builder.build_struct_gep(trait_handle, 1, "v_tmp").unwrap();
419+
builder.build_store(v_ptr, st_value);
420+
let type_hash = builder
421+
.build_struct_gep(trait_handle, 0, "tp_hash")
422+
.unwrap();
423+
let hash = st.get_type_code();
424+
let hash = builder.int_value(&PriType::U64, hash, false);
425+
builder.build_store(type_hash, hash);
426+
Ok(trait_handle)
427+
})
428+
});
424429
}
425430
#[allow(clippy::needless_return)]
426431
return Err(mismatch_err!(

src/ast/diag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub struct PLLabel {
213213
pub struct PLDiagRaw {
214214
code: DiagCode,
215215
help: Option<String>,
216-
labels: Vec<PLLabel>,
216+
pub labels: Vec<PLLabel>,
217217
pub source: Option<String>,
218218
}
219219
/// # PLDiag

src/ast/expects/builtin.pi.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

src/ast/expects/gc.pi.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

src/ast/expects/io.pi.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

src/ast/expects/panic.pi.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)