Skip to content

Commit 2fe33fb

Browse files
authored
Merge pull request #313 from Pivot-Studio/Chronostasys/issue312
fix #312
2 parents 251570a + 7d5c7c3 commit 2fe33fb

30 files changed

+852
-243
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Set default behavior to automatically normalize line endings.
22
* text=auto
3+
*.pi eol=lf

Cargo.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ lto = "fat"
9898
incremental = true
9999
codegen-units = 1
100100
# rpath = true
101+
102+
103+
[dev-dependencies]
104+
expect-test = "1.4.1"
105+

src/ast/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
## 注意事项
1414

15+
1516
- 修改`Ctx`结构体成员或者增加函数的时候,若不熟悉rust或者代码,容易出现生命周期问题。若出现此类问题,请按照以下三点依次尝试:
1617
- `Ctx`中所有直接或间接由`context`生成的类型(几乎都是inkwell库中的类型)的生命周期泛型应该为`'ctx`
1718
- 所有被borrow的字段的borrow的生命周期应该为`'a`,除了`context`字段(`context`生命周期是`'a`

src/ast/builder/llvmbuilder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ impl<'a, 'ctx> LLVMBuilder<'a, 'ctx> {
779779
) -> (DIType<'ctx>, u64) {
780780
let field_pltype = match field.typenode.get_type(ctx, &self.clone().into(), true) {
781781
Ok(field_pltype) => field_pltype,
782-
Err(_) => ctx.get_type("i64", Default::default()).unwrap(),
782+
Err(_) => ctx.get_type("i64", Default::default()).unwrap().tp,
783783
};
784784
let di_type = self.get_ditype(&field_pltype.borrow(), ctx);
785785
let debug_type = di_type.unwrap();

src/ast/compiler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::node::program::cycle_deps_recover;
12
use super::node::program::ModWrapper;
23
#[cfg(feature = "llvm")]
34
use crate::ast::jit_config::IS_JIT;
@@ -67,7 +68,7 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
6768
re
6869
}
6970

70-
#[salsa::tracked]
71+
#[salsa::tracked(recovery_fn=cycle_deps_recover)]
7172
pub fn compile_dry_file(db: &dyn Db, docs: FileCompileInput) -> Option<ModWrapper> {
7273
if docs.file(db).ends_with(".toml") {
7374
log::error!("lsp error: toml file {}", docs.file(db));

src/ast/ctx.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::node::node_result::NodeResult;
88
use super::node::NodeEnum;
99
use super::node::TypeNode;
1010
use super::plmod::CompletionItemWrapper;
11+
use super::plmod::GlobType;
1112
use super::plmod::GlobalVar;
1213
use super::plmod::LSPDef;
1314
use super::plmod::Mod;
@@ -54,8 +55,6 @@ use rustc_hash::FxHashMap;
5455
use rustc_hash::FxHashSet;
5556
use std::cell::RefCell;
5657

57-
use std::path::Path;
58-
5958
use std::path::PathBuf;
6059
use std::sync::Arc;
6160
mod builtins;
@@ -139,17 +138,10 @@ impl<'a, 'ctx> Ctx<'a> {
139138
config: Config,
140139
db: &'a dyn Db,
141140
) -> Ctx<'a> {
142-
let f = Path::new(Path::new(src_file_path).file_stem().unwrap())
143-
.file_name()
144-
.take()
145-
.unwrap()
146-
.to_str()
147-
.unwrap()
148-
.to_string();
149141
Ctx {
150142
need_highlight: 0,
151143
generic_types: FxHashMap::default(),
152-
plmod: Mod::new(f, src_file_path.to_string()),
144+
plmod: Mod::new(src_file_path.to_string()),
153145
father: None,
154146
init_func: None,
155147
function: None,
@@ -698,15 +690,15 @@ impl<'a, 'ctx> Ctx<'a> {
698690
},
699691
);
700692
}
701-
pub fn get_type(&self, name: &str, range: Range) -> Result<Arc<RefCell<PLType>>, PLDiag> {
693+
pub fn get_type(&self, name: &str, range: Range) -> Result<GlobType, PLDiag> {
702694
if let Some(pv) = self.generic_types.get(name) {
703695
self.set_if_refs_tp(pv.clone(), range);
704696
self.send_if_go_to_def(
705697
range,
706698
pv.borrow().get_range().unwrap_or(range),
707699
self.plmod.path.clone(),
708700
);
709-
return Ok(pv.clone());
701+
return Ok(pv.clone().into());
710702
}
711703
if let Ok(pv) = self.plmod.get_type(name, range, self) {
712704
return Ok(pv);
@@ -774,15 +766,15 @@ impl<'a, 'ctx> Ctx<'a> {
774766
}
775767
self.set_if_refs_tp(pltype.clone(), range);
776768
self.send_if_go_to_def(range, range, self.plmod.path.clone());
777-
self.plmod.types.insert(name, pltype);
769+
self.plmod.types.insert(name, pltype.into());
778770
Ok(())
779771
}
780772
pub fn add_type_without_check(&mut self, pltype: Arc<RefCell<PLType>>) {
781773
if let PLType::Generic(_) = &*pltype.borrow() {
782774
unreachable!()
783775
}
784776
let name = pltype.borrow().get_name();
785-
self.plmod.types.insert(name, pltype);
777+
self.plmod.types.insert(name, pltype.into());
786778
}
787779
#[inline]
788780
fn add_generic_type(&mut self, name: String, pltype: Arc<RefCell<PLType>>) {
@@ -1001,6 +993,9 @@ impl<'a, 'ctx> Ctx<'a> {
1001993
fn get_type_completions_in_ns(&self, ns: &str, m: &mut FxHashMap<String, CompletionItem>) {
1002994
self.with_ns(ns, |ns| {
1003995
for (k, v) in ns.types.iter() {
996+
if !v.visibal_outside() {
997+
continue;
998+
}
1004999
let mut insert_text = None;
10051000
let mut command = None;
10061001
let tp = match &*v.clone().borrow() {
@@ -1020,6 +1015,10 @@ impl<'a, 'ctx> Ctx<'a> {
10201015
}
10211016
_ => continue, // skip completion for primary types
10221017
};
1018+
if k.starts_with('|') {
1019+
// skip method
1020+
continue;
1021+
}
10231022
let mut item = CompletionItem {
10241023
label: k.to_string(),
10251024
kind: Some(tp),
@@ -1042,7 +1041,7 @@ impl<'a, 'ctx> Ctx<'a> {
10421041

10431042
pub fn get_type_completions(&self) -> Vec<CompletionItem> {
10441043
let mut m = FxHashMap::default();
1045-
self.get_pltp_completions(&mut m, |tp| !matches!(tp, PLType::Fn(_)));
1044+
self.get_pltp_completions(&mut m, |tp| !matches!(&*tp.borrow(), PLType::Fn(_)));
10461045
self.plmod.get_ns_completions_pri(&mut m);
10471046
m.values().cloned().collect()
10481047
}
@@ -1088,7 +1087,7 @@ impl<'a, 'ctx> Ctx<'a> {
10881087
fn get_pltp_completions(
10891088
&self,
10901089
vmap: &mut FxHashMap<String, CompletionItem>,
1091-
filter: impl Fn(&PLType) -> bool,
1090+
filter: impl Fn(&GlobType) -> bool,
10921091
) {
10931092
self.plmod
10941093
.get_pltp_completions(vmap, &filter, &self.generic_types, true);

src/ast/diag.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ define_error!(
136136
TUPLE_WRONG_DECONSTRUCT_PARAM_LEN = "tuple wrong deconstruct param len",
137137
DEF_DECONSTRUCT_MUST_HAVE_VALUE = "def deconstruct must have value",
138138
STRUCT_FIELD_NOT_EXISTS = "struct field not exists",
139+
TRY_TO_EXPORT_NON_REEXPORT_SYMBOL = "try to export non reexport symbol",
140+
CYCLE_DEPENDENCY = "cycle dependency not allowed",
139141
);
140142
macro_rules! define_warn {
141143
($(
@@ -230,6 +232,13 @@ impl Pos {
230232
}
231233
use std::fmt::Debug;
232234
impl PLDiag {
235+
#[cfg(test)]
236+
pub fn rm_file(&mut self) {
237+
self.raw.source = None;
238+
self.raw.labels.iter_mut().for_each(|label| {
239+
label.file = String::new();
240+
})
241+
}
233242
pub fn print(&self, path: &str, f: impl Fn(&dyn Db, &str) -> Source + 'static, db: &dyn Db) {
234243
let mut colors = ColorGenerator::new();
235244
let mut rb = Report::build(
@@ -263,8 +272,8 @@ impl PLDiag {
263272
if let Some((tpl, args)) = &label.txt {
264273
lab = Label::new((
265274
label.file.as_str(),
266-
label.range.start.utf8_offset(&f(db, path))
267-
..label.range.end.utf8_offset(&f(db, path)),
275+
label.range.start.utf8_offset(&f(db, label.file.as_str()))
276+
..label.range.end.utf8_offset(&f(db, label.file.as_str())),
268277
));
269278
let mut msg = tpl.clone();
270279
msg = msg.format(
@@ -276,9 +285,9 @@ impl PLDiag {
276285
lab = lab.with_message(msg);
277286
} else {
278287
lab = Label::new((
279-
path,
280-
label.range.start.utf8_offset(&f(db, path))
281-
..label.range.end.utf8_offset(&f(db, path)),
288+
label.file.as_str(),
289+
label.range.start.utf8_offset(&f(db, label.file.as_str()))
290+
..label.range.end.utf8_offset(&f(db, label.file.as_str())),
282291
));
283292
}
284293
rb = rb.with_label(lab.with_color(color));

src/ast/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ macro_rules! add_basic_types {
2828
paste::paste! {
2929
let [<pltype_$ident>] = PLType::Primitive(PriType::[<$ident:upper>]);
3030
$map
31-
.insert(stringify!($ident).to_string(), Arc::new(RefCell::new( [<pltype_$ident>])));
31+
.insert(stringify!($ident).to_string(),$crate::ast::plmod::GlobType::new([<pltype_$ident>]));
3232
}
3333
)*
3434
};

src/ast/node/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl Node for IsNode {
309309
);
310310
let cond = builder.try_load2var(Default::default(), cond, ctx).unwrap();
311311
let cond = builder.build_int_truncate(cond, &PriType::BOOL, "trunctemp");
312-
cond.new_output(ctx.get_type("bool", Default::default()).unwrap())
312+
cond.new_output(ctx.get_type("bool", Default::default()).unwrap().tp)
313313
.set_const()
314314
.to_result()
315315
} else {

0 commit comments

Comments
 (0)