Skip to content

Commit

Permalink
Add disabling support
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname committed Jul 17, 2024
1 parent db14a1e commit 8b7d044
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 10 additions & 0 deletions tool/src/demo_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ impl<'tcx> WebDemoGenerationContext<'tcx> {

{
let type_name = self.formatter.fmt_type_name(id);

let ty = self.tcx.resolve_type(id);
if ty.attrs().disable {
continue;
}

for method in methods {
if method.attrs.disable {
continue;
}

let val = RenderTerminusContext::evaluate_terminus(
self,
type_name.to_string(),
Expand Down
27 changes: 21 additions & 6 deletions tool/src/demo_gen/terminus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,41 @@ impl<'a, 'tcx> RenderTerminusContext<'a, 'tcx> {
self.ctx.formatter.fmt_primitive_list_type(*p).to_string()
}
Type::Slice(hir::Slice::Strs(..)) => "Array<String>".to_string(),
Type::Enum(e) => self
Type::Enum(e) => {
let type_name = self
.ctx
.formatter
.fmt_type_name(e.tcx_id.into())
.to_string(),
.to_string();

if e.resolve(&self.ctx.tcx).attrs.disable {
self.ctx.errors.push_error(format!("Found usage of disabled type {type_name}"))
}

type_name
},
_ => unreachable!("Unknown primitive type {:?}", param_type),
};

out_param(type_name);
}
Type::Opaque(o) => {
let attrs = &o.resolve(self.ctx.tcx).attrs.demo_attrs;

// We need to find a constructor that we can call.
// Piggybacking off of the #[diplomat::attr(constructor)] macro for now as well as test attributes in attrs.rs
let op = o.resolve(self.ctx.tcx);
let type_name = self.ctx.formatter.fmt_type_name(o.tcx_id.into());

let all_attrs = &o.resolve(self.ctx.tcx).attrs;
if all_attrs.disable {
self.ctx.errors.push_error(format!("Found usage of disabled type {type_name}"))
}
let attrs = &all_attrs.demo_attrs;

if attrs.external {
out_param(type_name.into());
return;
}

// We need to find a constructor that we can call.
// Piggybacking off of the #[diplomat::attr(constructor)] macro for now as well as test attributes in attrs.rs
let mut usable_constructor = false;

for method in op.methods.iter() {
Expand Down Expand Up @@ -283,6 +295,9 @@ impl<'a, 'tcx> RenderTerminusContext<'a, 'tcx> {
let st = s.resolve(self.ctx.tcx);

let type_name = self.ctx.formatter.fmt_type_name(s.tcx_id.into());
if st.attrs.disable {
self.ctx.errors.push_error(format!("Found usage of disabled type {type_name}"))
}

self.terminus_info
.imports
Expand Down
2 changes: 2 additions & 0 deletions tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ pub fn gen(
"demo-gen" => {
let mut attr_validator = hir::BasicAttributeValidator::new("demo-gen");

attr_validator.support.disabling = true;

// For finding default constructors of opaques:
attr_validator.support.constructors = true;
attr_validator.support.fallible_constructors = true;
Expand Down

0 comments on commit 8b7d044

Please sign in to comment.