Skip to content

Commit

Permalink
[move-compiler] New DiagnosticReporter for collecting diags (#20123)
Browse files Browse the repository at this point in the history
## Description 

- Followup to #20065
- Better encapsulates warning filter scopes with diagnostic reporting

## Test plan 

- ran tests

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
tnowacki authored and lxfind committed Nov 4, 2024
1 parent 3a37978 commit 975b871
Show file tree
Hide file tree
Showing 38 changed files with 627 additions and 3,457 deletions.
8 changes: 4 additions & 4 deletions external-crates/move/crates/move-compiler/src/cfgir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod visitor;
mod optimize;

use crate::{
diagnostics::warning_filters::WarningFiltersScope,
diagnostics::DiagnosticReporter,
expansion::ast::{Attributes, ModuleIdent, Mutability},
hlir::ast::{FunctionSignature, Label, SingleType, Var, Visibility},
shared::{program_info::TypingProgramInfo, unique_map::UniqueMap, CompilationEnv, Name},
Expand All @@ -28,7 +28,7 @@ use std::collections::BTreeSet;

pub struct CFGContext<'a> {
pub env: &'a CompilationEnv,
pub warning_filters_scope: WarningFiltersScope,
pub reporter: &'a DiagnosticReporter<'a>,
pub info: &'a TypingProgramInfo,
pub package: Option<Symbol>,
pub module: ModuleIdent,
Expand Down Expand Up @@ -56,11 +56,11 @@ pub fn refine_inference_and_verify(context: &CFGContext, cfg: &mut MutForwardCFG

impl CFGContext<'_> {
fn add_diag(&self, diag: crate::diagnostics::Diagnostic) {
self.env.add_diag(&self.warning_filters_scope, diag);
self.reporter.add_diag(diag);
}

fn add_diags(&self, diags: crate::diagnostics::Diagnostics) {
self.env.add_diags(&self.warning_filters_scope, diags);
self.reporter.add_diags(diags);
}
}

Expand Down
41 changes: 19 additions & 22 deletions external-crates/move/crates/move-compiler/src/cfgir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use crate::{
visitor::{CFGIRVisitor, CFGIRVisitorConstructor, CFGIRVisitorContext},
},
diag,
diagnostics::{
warning_filters::{WarningFilters, WarningFiltersScope},
Diagnostic, Diagnostics,
},
diagnostics::{warning_filters::WarningFilters, Diagnostic, DiagnosticReporter, Diagnostics},
expansion::ast::{Attributes, ModuleIdent, Mutability},
hlir::ast::{self as H, BlockLabel, Label, Value, Value_, Var},
ice_assert,
Expand Down Expand Up @@ -48,7 +45,7 @@ enum NamedBlockType {
struct Context<'env> {
env: &'env CompilationEnv,
info: &'env TypingProgramInfo,
warning_filters_scope: WarningFiltersScope,
reporter: DiagnosticReporter<'env>,
current_package: Option<Symbol>,
label_count: usize,
named_blocks: UniqueMap<BlockLabel, (Label, Label)>,
Expand All @@ -58,10 +55,10 @@ struct Context<'env> {

impl<'env> Context<'env> {
pub fn new(env: &'env CompilationEnv, info: &'env TypingProgramInfo) -> Self {
let warning_filters_scope = env.top_level_warning_filter_scope().clone();
let reporter = env.diagnostic_reporter_at_top_level();
Context {
env,
warning_filters_scope,
reporter,
info,
current_package: None,
label_count: 0,
Expand All @@ -71,19 +68,19 @@ impl<'env> Context<'env> {
}

pub fn add_diag(&self, diag: Diagnostic) {
self.env.add_diag(&self.warning_filters_scope, diag);
self.reporter.add_diag(diag);
}

pub fn add_diags(&self, diags: Diagnostics) {
self.env.add_diags(&self.warning_filters_scope, diags);
self.reporter.add_diags(diags);
}

pub fn push_warning_filter_scope(&mut self, filters: WarningFilters) {
self.warning_filters_scope.push(filters)
self.reporter.push_warning_filter_scope(filters)
}

pub fn pop_warning_filter_scope(&mut self) {
self.warning_filters_scope.pop()
self.reporter.pop_warning_filter_scope()
}

fn new_label(&mut self) -> Label {
Expand Down Expand Up @@ -495,7 +492,7 @@ fn constant_(
let fake_infinite_loop_starts = BTreeSet::new();
let function_context = super::CFGContext {
env: context.env,
warning_filters_scope: context.warning_filters_scope.clone(),
reporter: &context.reporter,
info: context.info,
package: context.current_package,
module,
Expand All @@ -509,7 +506,7 @@ fn constant_(
};
cfgir::refine_inference_and_verify(&function_context, &mut cfg);
ice_assert!(
context.env,
context.reporter,
num_previous_errors == context.env.count_diags(),
full_loc,
"{}",
Expand Down Expand Up @@ -658,7 +655,7 @@ fn function_body(

let function_context = super::CFGContext {
env: context.env,
warning_filters_scope: context.warning_filters_scope.clone(),
reporter: &context.reporter,
info: context.info,
package: context.current_package,
module,
Expand Down Expand Up @@ -1007,7 +1004,7 @@ fn visit_program(context: &mut Context, prog: &mut G::Program) {
struct AbsintVisitor;
struct AbsintVisitorContext<'a> {
env: &'a CompilationEnv,
warning_filters_scope: WarningFiltersScope,
reporter: DiagnosticReporter<'a>,
info: Arc<TypingProgramInfo>,
current_package: Option<Symbol>,
}
Expand All @@ -1016,10 +1013,10 @@ impl CFGIRVisitorConstructor for AbsintVisitor {
type Context<'a> = AbsintVisitorContext<'a>;

fn context<'a>(env: &'a CompilationEnv, program: &G::Program) -> Self::Context<'a> {
let warning_filters_scope = env.top_level_warning_filter_scope().clone();
let reporter = env.diagnostic_reporter_at_top_level();
AbsintVisitorContext {
env,
warning_filters_scope,
reporter,
info: program.info.clone(),
current_package: None,
}
Expand All @@ -1029,21 +1026,21 @@ impl CFGIRVisitorConstructor for AbsintVisitor {
impl AbsintVisitorContext<'_> {
#[allow(unused)]
fn add_diag(&self, diag: crate::diagnostics::Diagnostic) {
self.env.add_diag(&self.warning_filters_scope, diag);
self.reporter.add_diag(diag);
}

fn add_diags(&self, diags: crate::diagnostics::Diagnostics) {
self.env.add_diags(&self.warning_filters_scope, diags);
self.reporter.add_diags(diags);
}
}

impl<'a> CFGIRVisitorContext for AbsintVisitorContext<'a> {
fn push_warning_filter_scope(&mut self, filters: WarningFilters) {
self.warning_filters_scope.push(filters)
self.reporter.push_warning_filter_scope(filters)
}

fn pop_warning_filter_scope(&mut self) {
self.warning_filters_scope.pop()
self.reporter.pop_warning_filter_scope()
}

fn visit_module_custom(&mut self, _ident: ModuleIdent, mdef: &G::ModuleDefinition) -> bool {
Expand Down Expand Up @@ -1079,7 +1076,7 @@ impl<'a> CFGIRVisitorContext for AbsintVisitorContext<'a> {
let (cfg, infinite_loop_starts) = ImmForwardCFG::new(*start, blocks, block_info.iter());
let function_context = super::CFGContext {
env: self.env,
warning_filters_scope: self.warning_filters_scope.clone(),
reporter: &self.reporter,
info: &self.info,
package: self.current_package,
module: mident,
Expand Down
15 changes: 8 additions & 7 deletions external-crates/move/crates/move-compiler/src/cfgir/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,31 +327,32 @@ macro_rules! simple_visitor {
pub struct $visitor;

pub struct Context<'a> {
#[allow(unused)]
env: &'a crate::shared::CompilationEnv,
warning_filters_scope: crate::diagnostics::warning_filters::WarningFiltersScope,
reporter: crate::diagnostics::DiagnosticReporter<'a>,
}

impl crate::cfgir::visitor::CFGIRVisitorConstructor for $visitor {
type Context<'a> = Context<'a>;

fn context<'a>(env: &'a crate::shared::CompilationEnv, _program: &crate::cfgir::ast::Program) -> Self::Context<'a> {
let warning_filters_scope = env.top_level_warning_filter_scope().clone();
let reporter = env.diagnostic_reporter_at_top_level();
Context {
env,
warning_filters_scope,
reporter,
}
}
}

impl Context<'_> {
#[allow(unused)]
fn add_diag(&self, diag: crate::diagnostics::Diagnostic) {
self.env.add_diag(&self.warning_filters_scope, diag);
self.reporter.add_diag(diag);
}

#[allow(unused)]
fn add_diags(&self, diags: crate::diagnostics::Diagnostics) {
self.env.add_diags(&self.warning_filters_scope, diags);
self.reporter.add_diags(diags);
}
}

Expand All @@ -360,11 +361,11 @@ macro_rules! simple_visitor {
&mut self,
filters: crate::diagnostics::warning_filters::WarningFilters,
) {
self.warning_filters_scope.push(filters)
self.reporter.push_warning_filter_scope(filters)
}

fn pop_warning_filter_scope(&mut self) {
self.warning_filters_scope.pop()
self.reporter.pop_warning_filter_scope()
}

$($overrides)*
Expand Down
Loading

0 comments on commit 975b871

Please sign in to comment.