Skip to content

Commit

Permalink
Rework Used tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Sep 14, 2023
1 parent 31d81a9 commit fa435cf
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 54 deletions.
46 changes: 30 additions & 16 deletions crates/rune/src/compile/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::compile::{
use crate::hir;
use crate::macros::Storage;
use crate::parse::Resolve;
use crate::query::{Build, BuildEntry, GenericsParameters, Query};
use crate::query::{Build, BuildEntry, GenericsParameters, Query, Used};
use crate::runtime::unit::UnitEncoder;
use crate::shared::{Consts, Gen};
use crate::worker::{LoadFileKind, Task, Worker};
Expand Down Expand Up @@ -146,11 +146,7 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
entry: BuildEntry,
unit_storage: &mut dyn UnitEncoder,
) -> compile::Result<()> {
let BuildEntry {
item_meta,
build,
used,
} = entry;
let BuildEntry { item_meta, build } = entry;

let location = item_meta.location;

Expand All @@ -160,6 +156,12 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
Build::Query => {
tracing::trace!("query: {}", self.q.pool.item(item_meta.item));

let used = if self.q.is_used(&item_meta) {
Used::Used
} else {
Used::Unused
};

if self
.q
.query_meta(&item_meta.location, item_meta.item, used)?
Expand Down Expand Up @@ -190,7 +192,7 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
let mut c = self.compiler1(location, span, &mut asm);
assemble::fn_from_item_fn(&mut c, &hir, false)?;

if used.is_unused() {
if !self.q.is_used(&item_meta) {
self.q.diagnostics.not_used(location.source_id, span, None);
} else {
self.q.unit.new_function(
Expand Down Expand Up @@ -225,7 +227,7 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
let mut c = self.compiler1(location, span, &mut asm);
assemble::fn_from_item_fn(&mut c, &hir, false)?;

if used.is_unused() {
if !self.q.is_used(&item_meta) {
self.q.diagnostics.not_used(location.source_id, span, None);
} else {
self.q.unit.new_function(
Expand Down Expand Up @@ -278,7 +280,7 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
let hir = hir::lowering::item_fn(&mut cx, &f.ast)?;
assemble::fn_from_item_fn(&mut c, &hir, true)?;

if used.is_unused() {
if !c.q.is_used(&item_meta) {
c.q.diagnostics.not_used(location.source_id, &f.ast, None);
} else {
let name = f.ast.name.resolve(resolve_context!(self.q))?;
Expand Down Expand Up @@ -319,7 +321,7 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
let mut c = self.compiler1(location, &closure.ast, &mut asm);
assemble::expr_closure_secondary(&mut c, &hir, &closure.ast)?;

if used.is_unused() {
if !c.q.is_used(&item_meta) {
c.q.diagnostics
.not_used(location.source_id, &location.span, None);
} else {
Expand Down Expand Up @@ -351,7 +353,7 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
let mut c = self.compiler1(location, &b.ast, &mut asm);
assemble::async_block_secondary(&mut c, &hir)?;

if used.is_unused() {
if !self.q.is_used(&item_meta) {
self.q
.diagnostics
.not_used(location.source_id, &location.span, None);
Expand Down Expand Up @@ -381,12 +383,18 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
Build::Import(import) => {
tracing::trace!("import: {}", self.q.pool.item(item_meta.item));

let used = if self.q.is_used(&item_meta) {
Used::Used
} else {
Used::Unused
};

// Issue the import to check access.
let result = self
.q
.import(&location, item_meta.module, item_meta.item, used)?;
let result =
self.q
.import(&location, item_meta.module, item_meta.item, used, used)?;

if used.is_unused() {
if !self.q.is_used(&item_meta) {
self.q
.diagnostics
.not_used(location.source_id, &location.span, None);
Expand Down Expand Up @@ -417,9 +425,15 @@ impl<'arena> CompileBuildEntry<'_, 'arena> {
Build::ReExport => {
tracing::trace!("re-export: {}", self.q.pool.item(item_meta.item));

let used = if self.q.is_used(&item_meta) {
Used::Used
} else {
Used::Unused
};

let Some(import) =
self.q
.import(&location, item_meta.module, item_meta.item, used)?
.import(&location, item_meta.module, item_meta.item, used, used)?
else {
return Err(compile::Error::new(
location.span,
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::hash::{Hash, ParametersBuilder};
use crate::hir;
use crate::indexing;
use crate::parse::Resolve;
use crate::query::{self, Build, BuildEntry, GenericsParameters, Named, Query, Used};
use crate::query::{self, Build, BuildEntry, GenericsParameters, Named, Query};
use crate::runtime::{Type, TypeCheck};
use crate::SourceId;

Expand Down Expand Up @@ -270,13 +270,13 @@ fn expr_call_closure<'hir>(
expr(cx, &ast.body)?;
let layer = cx.scopes.pop().with_span(&ast.body)?;

cx.q.set_used(&meta.item_meta);
cx.q.inner.queue.push_back(BuildEntry {
item_meta: meta.item_meta,
build: Build::Closure(indexing::Closure {
ast: Box::new(ast.clone()),
call,
}),
used: Used::Used,
});

cx.q.insert_captures(meta.hash, layer.captures());
Expand Down Expand Up @@ -925,13 +925,13 @@ pub(crate) fn expr_block<'hir>(

cx.q.insert_captures(meta.hash, layer.captures());

cx.q.set_used(&meta.item_meta);
cx.q.inner.queue.push_back(BuildEntry {
item_meta: meta.item_meta,
build: Build::AsyncBlock(indexing::AsyncBlock {
ast: ast.block.clone(),
call,
}),
used: Used::Used,
});

iter!(layer.captures())
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/indexing/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@ pub(crate) fn item_fn_immediate(
if is_test {
return Err(compile::Error::msg(
&ast,
"The #[test] attribute is not supported on member functions",
"The #[test] attribute is not supported on functions receiving `self`",
));
}

if is_bench {
return Err(compile::Error::msg(
&ast,
"The #[bench] attribute is not supported on member functions",
"The #[bench] attribute is not supported on functions receiving `self`",
));
}

Expand All @@ -831,9 +831,9 @@ pub(crate) fn item_fn_immediate(
}),
});
} else {
// NB: it's only a public item in the sense of exporting it if it's not
// NB: It's only a public item in the sense of exporting it if it's not
// inside of a nested item.
let is_public = item_meta.is_public(idx.q.pool) && idx.nested_item.is_none();
let is_exported = item_meta.is_public(idx.q.pool) && idx.nested_item.is_none();

let entry = indexing::Entry {
item_meta,
Expand All @@ -846,7 +846,7 @@ pub(crate) fn item_fn_immediate(
}),
};

if is_public || is_test || is_bench {
if is_exported || is_test || is_bench {
idx.q.index_and_build(entry);
} else {
idx.q.index(entry);
Expand Down
2 changes: 0 additions & 2 deletions crates/rune/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ pub(crate) enum Build {
pub(crate) struct BuildEntry {
/// The item of the build entry.
pub(crate) item_meta: ItemMeta,
/// If the queued up entry was unused or not.
pub(crate) used: Used,
/// The build entry.
pub(crate) build: Build,
}
Expand Down
Loading

0 comments on commit fa435cf

Please sign in to comment.