Skip to content

Commit

Permalink
Do not produce STUB section in the Erlang Service linter
Browse files Browse the repository at this point in the history
Summary: Now that the STUB is unused, get rid of it and simply use the AST.

Reviewed By: VLanvin

Differential Revision: D64114847

fbshipit-source-id: 72bd33901ac7ab822db02edffb48e4b48ec2a866
  • Loading branch information
robertoaloi authored and facebook-github-bot committed Oct 10, 2024
1 parent c06351a commit bcd8a60
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 169 deletions.
5 changes: 0 additions & 5 deletions crates/eqwalizer/src/ast/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ pub trait EqwalizerErlASTStorage {
project_id: ProjectId,
module: ModuleName,
) -> Result<Arc<Vec<u8>>, Error>;
fn get_erl_stub_bytes(
&self,
project_id: ProjectId,
module: ModuleName,
) -> Result<Arc<Vec<u8>>, Error>;
}

#[salsa::query_group(EqwalizerASTDatabaseStorage)]
Expand Down
12 changes: 0 additions & 12 deletions crates/erlang_service/fixtures/doc_attributes_stripped.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ AST
{eof,{294,294}}.


STUB
{attribute,{0,0},file,{"fixtures/doc_attributes_stripped.erl",0}}.
{attribute,{0,32},module,doc_attributes_stripped}.
{attribute,{35,52},export,[{main,0}]}.
{attribute,{256,274},
spec,
{{main,0},
[{type,{266,274},
'fun',
[{type,{266,274},product,[]},{atom,{272,274},ok}]}]}}.


WARNINGS
[]

Expand Down
29 changes: 0 additions & 29 deletions crates/erlang_service/fixtures/error.expected
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,6 @@ AST
{eof,{485,485}}.


STUB
{attribute,{0,0},file,{"fixtures/error.erl",0}}.
{attribute,{0,14},module,error}.
{attribute,{17,46},
spec,
{{unrecognized1,0},
[{type,{36,46},
'fun',
[{type,{36,46},product,[]},{atom,{42,46},atom}]}]}}.
{attribute,{125,154},
spec,
{{unrecognized2,0},
[{type,{144,154},
'fun',
[{type,{144,154},product,[]},{atom,{150,154},atom}]}]}}.
{attribute,{242,271},
spec,
{{unrecognized3,0},
[{type,{261,271},
'fun',
[{type,{261,271},product,[]},{atom,{267,271},atom}]}]}}.
{attribute,{377,406},
spec,
{{unrecognized4,0},
[{type,{396,406},
'fun',
[{type,{396,406},product,[]},{atom,{402,406},atom}]}]}}.


WARNINGS
[
ParseError {
Expand Down
5 changes: 0 additions & 5 deletions crates/erlang_service/fixtures/error_attr.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ AST
{eof,{87,87}}.


STUB
{attribute,{0,0},file,{"fixtures/error_attr.erl",0}}.
{attribute,{0,19},module,error_attr}.


WARNINGS
[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ AST
{eof,{250,250}}.


STUB
{attribute,{0,0},file,{"fixtures/misplaced_comment_error.erl",0}}.
{attribute,{0,32},module,misplaced_comment_error}.


WARNINGS
[
ParseError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ AST
{eof,{166,166}}.


STUB
{attribute,{0,0},file,{"fixtures/override_warn_missing_spec_all.erl",0}}.
{attribute,{0,39},module,override_warn_missing_spec_all}.
{attribute,{41,74},compile,nowarn_missing_spec_all}.
{attribute,{77,94},export,[{main,0}]}.
{attribute,{97,115},
spec,
{{main,0},
[{type,{107,115},
'fun',
[{type,{107,115},product,[]},{atom,{113,115},ok}]}]}}.


WARNINGS
[
ParseError {
Expand Down
5 changes: 0 additions & 5 deletions crates/erlang_service/fixtures/regular.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ AST
{eof,{18,18}}.


STUB
{attribute,{0,0},file,{"fixtures/regular.erl",0}}.
{attribute,{0,16},module,regular}.


WARNINGS
[]

Expand Down
5 changes: 0 additions & 5 deletions crates/erlang_service/fixtures/structured_comment.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ AST
{eof,{128,128}}.


STUB
{attribute,{0,0},file,{"fixtures/structured_comment.erl",0}}.
{attribute,{0,27},module,structured_comment}.


WARNINGS
[
ParseError {
Expand Down
3 changes: 0 additions & 3 deletions crates/erlang_service/fixtures/unsupported_extension.expected
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
AST


STUB


WARNINGS
[]

Expand Down
8 changes: 0 additions & 8 deletions crates/erlang_service/fixtures/unused_record.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ AST
{eof,{57,57}}.


STUB
{attribute,{0,0},file,{"fixtures/unused_record.erl",0}}.
{attribute,{0,22},module,unused_record}.
{attribute,{25,55},
record,
{my_record,[{record_field,{45,53},{atom,{45,53},my_field}}]}}.


WARNINGS
[
ParseError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ AST
{eof,{32,32}}.


STUB
{attribute,{0,0},file,{"fixtures/unused_record_in_header.hrl",0}}.
{attribute,{0,30},
record,
{my_record,[{record_field,{20,28},{atom,{20,28},my_field}}]}}.


WARNINGS
[]

Expand Down
15 changes: 4 additions & 11 deletions crates/erlang_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ impl Response {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParseResult {
pub ast: Arc<Vec<u8>>,
pub stub: Arc<Vec<u8>>,
pub errors: Vec<ParseError>,
pub warnings: Vec<ParseError>,
}
Expand All @@ -299,7 +298,6 @@ impl ParseResult {
pub fn error(error: ParseError) -> Self {
Self {
ast: Arc::default(),
stub: Arc::default(),
errors: vec![error],
warnings: Vec::default(),
}
Expand Down Expand Up @@ -429,15 +427,13 @@ impl Connection {
});

let mut ast = vec![];
let mut stub = vec![];
let mut warnings = vec![];
let mut errors = vec![];

reply
.decode_segments(|tag, data| {
match tag {
b"AST" => ast = data,
b"STU" => stub = data,
b"WAR" => warnings = data,
b"ERR" => errors = data,
_ => log::error!("unrecognised segment {:?}", tag),
Expand All @@ -447,7 +443,6 @@ impl Connection {
.and_then(|()| {
Ok(ParseResult {
ast: Arc::new(ast),
stub: Arc::new(stub),
warnings: decode_errors(&warnings).context("decoding warnings")?,
errors: decode_errors(&errors).context("decoding errors")?,
})
Expand Down Expand Up @@ -1057,10 +1052,9 @@ mod tests {
};
let response = CONN.request_parse(request, || (), &|_, _, _| None);
let ast = str::from_utf8(&response.ast).unwrap();
let stub = str::from_utf8(&response.stub).unwrap();
let actual = format!(
"AST\n{}\n\nSTUB\n{}\n\nWARNINGS\n{:#?}\n\nERRORS\n{:#?}\n",
ast, stub, &response.warnings, &response.errors
"AST\n{}\n\nWARNINGS\n{:#?}\n\nERRORS\n{:#?}\n",
ast, &response.warnings, &response.errors
);
expected.assert_eq(&actual);
}
Expand All @@ -1086,7 +1080,6 @@ mod tests {
};
let response = CONN.request_parse(request, || (), &|_, _, _| None);
let ast = str::from_utf8(&response.ast).unwrap();
let stub = str::from_utf8(&response.stub).unwrap();
let errors = &response
.errors
.iter()
Expand All @@ -1097,8 +1090,8 @@ mod tests {
})
.collect::<Vec<ParseError>>();
let actual = format!(
"AST\n{}\n\nSTUB\n{}\n\nWARNINGS\n{:#?}\n\nERRORS\n{:#?}\n",
ast, stub, &response.warnings, &errors
"AST\n{}\n\nWARNINGS\n{:#?}\n\nERRORS\n{:#?}\n",
ast, &response.warnings, &errors
);
expected.assert_eq(&actual);
}
Expand Down
22 changes: 0 additions & 22 deletions crates/ide_db/src/eqwalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,28 +389,6 @@ impl EqwalizerErlASTStorage for crate::RootDatabase {
Err(Error::ModuleNotFound(module.as_str().into()))
}
}

fn get_erl_stub_bytes(
&self,
project_id: ProjectId,
module: ModuleName,
) -> Result<Arc<Vec<u8>>, Error> {
if let Some(file_id) = self.module_index(project_id).file_for_module(&module) {
let result = self.module_ast(
file_id,
elp_erlang_service::Format::OffsetEtf,
vec![],
vec![],
);
if result.is_ok() {
Ok(result.stub.clone())
} else {
Err(Error::ParseError)
}
} else {
Err(Error::ModuleNotFound(module.as_str().into()))
}
}
}

impl elp_eqwalizer::DbApi for crate::RootDatabase {
Expand Down
57 changes: 18 additions & 39 deletions erlang_service/src/erlang_service_lint.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,25 @@ run(Id, [FileName, FileId, Options0, OverrideOptions, FileText, PostProcess, Det
ElpMetadata ->
elp_metadata:insert_metadata(ElpMetadata, Forms2)
end,
StripDocAttributes = should_strip_doc_attributes(),
AST = case should_strip_doc_attributes() of
true ->
filter_doc_attributes(Forms3);
false ->
Forms3
end,
ResultAST = PostProcess(AST, FileName),
case lint_file(Forms3, FileName, Options3, OverrideOptions) of
{ok, []} ->
{Stub, AST} = partition_stub(Forms3, StripDocAttributes),
ResultStub = PostProcess(Stub, FileName),
ResultAST = PostProcess(AST, FileName),
{ok, [{<<"AST">>, ResultAST}, {<<"STU">>, ResultStub}]};
{ok, [{<<"AST">>, ResultAST}]};
{ok, Warnings} ->
{Stub, AST} = partition_stub(Forms3, StripDocAttributes),
ResultStub = PostProcess(Stub, FileName),
ResultAST = PostProcess(AST, FileName),
FormattedWarnings = format_errors(Forms3, FileName, Warnings),
{ok, [
{<<"AST">>, ResultAST},
{<<"STU">>, ResultStub},
{<<"WAR">>, FormattedWarnings}
]};
{ok, [{<<"AST">>, ResultAST},
{<<"WAR">>, FormattedWarnings}]};
{error, Errors, Warnings} ->
{Stub, AST} = partition_stub(Forms3, StripDocAttributes),
ResultStub = PostProcess(Stub, FileName),
ResultAST = PostProcess(AST, FileName),
FormattedErrors = format_errors(Forms3, FileName, Errors),
FormattedWarnings = format_errors(Forms3, FileName, Warnings),
{ok, [
{<<"AST">>, ResultAST},
{<<"STU">>, ResultStub},
{<<"ERR">>, FormattedErrors},
{<<"WAR">>, FormattedWarnings}
]}
Expand Down Expand Up @@ -162,29 +155,15 @@ vararg_transform(List) when is_list(List) ->
vararg_transform(Atomic) ->
Atomic.

-spec partition_stub([elp_parse:abstract_form()], boolean()) ->
{Stub :: [elp_parse:abstract_form()], AST :: [elp_parse:abstract_form()]}.
partition_stub(Forms, StripDocAttributes) ->
partition_stub(Forms, StripDocAttributes, {[], []}).
-spec filter_doc_attributes([elp_parse:abstract_form()]) -> [elp_parse:abstract_form()].
filter_doc_attributes(Forms) ->
[Form || Form <- Forms, not is_doc_attribute(Form)].

-spec partition_stub(
[elp_parse:abstract_form()], boolean(), {[elp_parse:abstract_form()], [elp_parse:abstract_form()]}
) ->
{Stub :: [elp_parse:abstract_form()], AST :: [elp_parse:abstract_form()]}.
partition_stub([], _StripDocAttributes, {Stub, AST}) ->
{lists:reverse(Stub), lists:reverse(AST)};
partition_stub([{attribute, _Anno, Attr, _Meta} | Forms], true, Acc) when
Attr =:= doc; Attr =:= moduledoc; Attr =:= docformat
->
% Skip EEP059 doc attributes, to model the behaviour of the Erlang/OTP compiler
% https://github.com/erlang/otp/blob/f2f48e329827b1750a39cced3cd4a27183e944af/lib/compiler/src/compile.erl#L816
partition_stub(Forms, true, Acc);
partition_stub([{attribute, _Anno, _Attr, _Meta} = Form | Forms], StripDocAttributes, {Stub, AST}) ->
% Found an attribute. Include it in both the STUB and the AST
partition_stub(Forms, StripDocAttributes, {[Form | Stub], [Form | AST]});
partition_stub([Form | Forms], StripDocAttributes, {Stub, AST}) ->
% Only attributes are relevant for EqWAlizer (which uses the STUB), so only include other forms in the AST only
partition_stub(Forms, StripDocAttributes, {Stub, [Form | AST]}).
-spec is_doc_attribute(elp_parse:abstract_form()) -> boolean().
is_doc_attribute({attribute, _Anno, Attr, _Meta}) when Attr =:= doc; Attr =:= moduledoc; Attr =:= docformat ->
true;
is_doc_attribute(_) ->
false.

format_errors(Forms, OriginalPath, Warnings) ->
Formatted =
Expand Down

0 comments on commit bcd8a60

Please sign in to comment.