Skip to content

Commit

Permalink
Update HIR for doc attribute
Browse files Browse the repository at this point in the history
Summary: Create a new `DocAttribute` in HIR, and add it to `FormList`

Reviewed By: robertoaloi

Differential Revision: D57614531

fbshipit-source-id: 5e7823d1351b79f22b573e642abed6e467dd985f
  • Loading branch information
alanz authored and facebook-github-bot committed Jun 27, 2024
1 parent f2be41c commit 6392db3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/hir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,7 @@ bar() ->
Form::Record(_) => acc,
Form::Attribute(_) => acc,
Form::ModuleDocAttribute(_) => acc,
Form::DocAttribute(_) => acc,
Form::CompileOption(_) => acc,
Form::DeprecatedAttribute(_) => acc,
Form::FeatureAttribute(_) => acc,
Expand Down
25 changes: 25 additions & 0 deletions crates/hir/src/form_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ impl FormList {
self.data.module_doc_attributes.iter()
}

pub fn doc_attributes(&self) -> impl Iterator<Item = (DocAttributeId, &DocAttribute)> {
self.data.doc_attributes.iter()
}

pub fn pp_stack(&self) -> &Arena<PPDirective> {
&self.data.pp_directives
}
Expand Down Expand Up @@ -197,6 +201,7 @@ impl FormList {
FormIdx::Record(idx) => Form::Record(&self[idx]),
FormIdx::Attribute(idx) => Form::Attribute(&self[idx]),
FormIdx::ModuleDocAttribute(idx) => Form::ModuleDocAttribute(&self[idx]),
FormIdx::DocAttribute(idx) => Form::DocAttribute(&self[idx]),
FormIdx::CompileOption(idx) => Form::CompileOption(&self[idx]),
FormIdx::DeprecatedAttribute(idx) => Form::DeprecatedAttribute(&self[idx]),
FormIdx::FeatureAttribute(idx) => Form::FeatureAttribute(&self[idx]),
Expand Down Expand Up @@ -230,6 +235,7 @@ pub(crate) struct FormListData {
attributes: Arena<Attribute>,
feature_attributes: Arena<FeatureAttribute>,
module_doc_attributes: Arena<ModuleDocAttribute>,
doc_attributes: Arena<DocAttribute>,
compile_options: Arena<CompileOption>,
record_fields: Arena<RecordField>,
fa_entries: Arena<FaEntry>,
Expand Down Expand Up @@ -257,6 +263,7 @@ impl FormListData {
records,
attributes,
module_doc_attributes,
doc_attributes,
feature_attributes,
compile_options,
record_fields,
Expand All @@ -281,6 +288,7 @@ impl FormListData {
compile_options.shrink_to_fit();
attributes.shrink_to_fit();
module_doc_attributes.shrink_to_fit();
doc_attributes.shrink_to_fit();
feature_attributes.shrink_to_fit();
record_fields.shrink_to_fit();
fa_entries.shrink_to_fit();
Expand All @@ -305,6 +313,7 @@ pub enum FormIdx {
Record(RecordId),
Attribute(AttributeId),
ModuleDocAttribute(ModuleDocAttributeId),
DocAttribute(DocAttributeId),
CompileOption(CompileOptionId),
DeprecatedAttribute(DeprecatedAttributeId),
FeatureAttribute(FeatureAttributeId),
Expand All @@ -327,6 +336,7 @@ pub enum Form<'a> {
Record(&'a Record),
Attribute(&'a Attribute),
ModuleDocAttribute(&'a ModuleDocAttribute),
DocAttribute(&'a DocAttribute),
CompileOption(&'a CompileOption),
DeprecatedAttribute(&'a DeprecatedAttribute),
FeatureAttribute(&'a FeatureAttribute),
Expand All @@ -349,6 +359,7 @@ pub type OptionalCallbacksId = Idx<OptionalCallbacks>;
pub type RecordId = Idx<Record>;
pub type AttributeId = Idx<Attribute>;
pub type ModuleDocAttributeId = Idx<ModuleDocAttribute>;
pub type DocAttributeId = Idx<DocAttribute>;
pub type CompileOptionId = Idx<CompileOption>;
pub type RecordFieldId = Idx<RecordField>;
pub type FaEntryId = Idx<FaEntry>;
Expand Down Expand Up @@ -491,6 +502,14 @@ impl Index<ModuleDocAttributeId> for FormList {
}
}

impl Index<DocAttributeId> for FormList {
type Output = DocAttribute;

fn index(&self, index: DocAttributeId) -> &Self::Output {
&self.data.doc_attributes[index]
}
}

impl Index<FeatureAttributeId> for FormList {
type Output = FeatureAttribute;

Expand Down Expand Up @@ -849,6 +868,12 @@ pub struct ModuleDocAttribute {
pub form_id: FormId<ast::WildAttribute>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DocAttribute {
pub cond: Option<PPConditionId>,
pub form_id: FormId<ast::WildAttribute>,
}

// ---------------------------------------------------------------------

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down
5 changes: 5 additions & 0 deletions crates/hir/src/form_list/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use la_arena::RawIdx;
use profile::Count;

use super::form_id::FormIdMap;
use super::DocAttribute;
use super::FeatureAttribute;
use super::FormIdx;
use super::FormListData;
Expand Down Expand Up @@ -632,6 +633,10 @@ impl<'a> Ctx<'a> {
Some(FormIdx::ModuleDocAttribute(
self.data.module_doc_attributes.alloc(res),
))
} else if name == known::doc {
let form_id = self.id_map.get_id(attribute);
let res = DocAttribute { cond, form_id };
Some(FormIdx::DocAttribute(self.data.doc_attributes.alloc(res)))
} else {
let form_id = self.id_map.get_id(attribute);
let res = Attribute {
Expand Down
6 changes: 6 additions & 0 deletions crates/hir/src/form_list/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::fmt::Write as _;
use la_arena::IdxRange;
use la_arena::RawIdx;

use super::DocAttribute;
use super::FeatureAttribute;
use super::ModuleDocAttribute;
use crate::form_list::DeprecatedAttribute;
Expand Down Expand Up @@ -82,6 +83,7 @@ impl<'a> Printer<'a> {
FormIdx::ModuleDocAttribute(idx) => {
self.print_module_doc_attribute(&self.forms[idx])?
}
FormIdx::DocAttribute(idx) => self.print_doc_attribute(&self.forms[idx])?,
FormIdx::FeatureAttribute(idx) => self.print_feature_attribute(&self.forms[idx])?,
FormIdx::DeprecatedAttribute(idx) => self.print_deprecated(&self.forms[idx])?,
}
Expand Down Expand Up @@ -365,6 +367,10 @@ impl<'a> Printer<'a> {
)
}

fn print_doc_attribute(&mut self, attribute: &DocAttribute) -> fmt::Result {
writeln!(self, "-doc(...). %% cond: {:?}", raw_cond(&attribute.cond))
}

fn print_feature_attribute(&mut self, attribute: &FeatureAttribute) -> fmt::Result {
writeln!(
self,
Expand Down
12 changes: 12 additions & 0 deletions crates/hir/src/form_list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ Convenience functions for encoding and decoding from base64.
)
}

#[test]
fn doc_attribute() {
check(
r#"
-doc "Example".
"#,
expect![[r#"
-doc(...). %% cond: None
"#]],
)
}

#[test]
fn function() {
check(
Expand Down
1 change: 1 addition & 0 deletions crates/hir/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,6 @@ pub mod known {
author,
oncall,
moduledoc,
doc,
);
}
1 change: 1 addition & 0 deletions crates/ide/src/diagnostics/unused_include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ fn is_file_used(
FormIdx::DeprecatedAttribute(_) => return true,
FormIdx::FeatureAttribute(_) => return true,
FormIdx::ModuleDocAttribute(_) => {}
FormIdx::DocAttribute(_) => {}
FormIdx::FunctionClause(_) => {}
FormIdx::PPDirective(_) => {}
FormIdx::PPCondition(_) => {}
Expand Down

0 comments on commit 6392db3

Please sign in to comment.