Skip to content

Commit 7fca533

Browse files
authored
refactor: module diagnostic action move to Diagnosable trait (#9264)
* refactor: Diagnosable trait use mutable self * refactor: update Diagnosable trait
1 parent 519c54c commit 7fca533

File tree

22 files changed

+85
-226
lines changed

22 files changed

+85
-226
lines changed

crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ mod t {
145145

146146
use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip};
147147
use rspack_collections::Identifiable;
148-
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
148+
use rspack_error::{impl_empty_diagnosable_trait, Result};
149149
use rspack_macros::impl_source_map_config;
150150
use rspack_sources::Source;
151151
use rspack_util::{atom::Atom, source_map::SourceMapKind};
@@ -285,10 +285,6 @@ mod t {
285285
todo!()
286286
}
287287

288-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
289-
todo!()
290-
}
291-
292288
fn original_source(&self) -> Option<&dyn Source> {
293289
todo!()
294290
}

crates/rspack_core/src/compiler/make/repair/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ impl Task<MakeTaskContext> for BuildTask {
6161

6262
let build_result = result.map(|t| {
6363
let diagnostics = module
64-
.clone_diagnostics()
65-
.into_iter()
64+
.diagnostics()
65+
.iter()
66+
.cloned()
6667
.map(|d| d.with_module_identifier(Some(module.identifier())))
6768
.collect();
6869
t.with_diagnostic(diagnostics)

crates/rspack_core/src/concatenated_module.rs

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
collections::hash_map::Entry,
44
fmt::Debug,
55
hash::{BuildHasherDefault, Hasher},
6-
sync::{Arc, LazyLock, Mutex},
6+
sync::{Arc, LazyLock},
77
};
88

99
use dashmap::DashMap;
@@ -372,7 +372,7 @@ pub struct ConcatenatedModule {
372372
#[cacheable(with=AsMap)]
373373
cached_source_sizes: DashMap<SourceType, f64, BuildHasherDefault<FxHasher>>,
374374
#[cacheable(with=Skip)]
375-
diagnostics: Mutex<Vec<Diagnostic>>,
375+
diagnostics: Vec<Diagnostic>,
376376
build_info: BuildInfo,
377377
}
378378

@@ -394,7 +394,7 @@ impl ConcatenatedModule {
394394
dependencies: vec![],
395395
blocks: vec![],
396396
cached_source_sizes: DashMap::default(),
397-
diagnostics: Mutex::new(vec![]),
397+
diagnostics: vec![],
398398
build_info: BuildInfo {
399399
cacheable: true,
400400
hash: None,
@@ -490,11 +490,6 @@ impl Module for ConcatenatedModule {
490490
&ModuleType::JsEsm
491491
}
492492

493-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
494-
let guard = self.diagnostics.lock().expect("should have diagnostics");
495-
guard.clone()
496-
}
497-
498493
fn factory_meta(&self) -> Option<&FactoryMeta> {
499494
self.root_module_ctxt.factory_meta.as_ref()
500495
}
@@ -556,9 +551,6 @@ impl Module for ConcatenatedModule {
556551
compilation: Option<&Compilation>,
557552
) -> Result<BuildResult> {
558553
let compilation = compilation.expect("should pass compilation");
559-
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L774-L784
560-
// Some fields does not exists in rspack
561-
self.clear_diagnostics();
562554

563555
let module_graph = compilation.get_module_graph();
564556
let modules = self
@@ -592,12 +584,8 @@ impl Module for ConcatenatedModule {
592584
for b in module.get_blocks() {
593585
self.blocks.push(*b);
594586
}
595-
let mut diagnostics_guard = self.diagnostics.lock().expect("should have diagnostics");
596587
// populate diagnostic
597-
diagnostics_guard.extend(module.get_diagnostics());
598-
599-
// release guard ASAP
600-
drop(diagnostics_guard);
588+
self.diagnostics.extend(module.diagnostics().into_owned());
601589

602590
// populate topLevelDeclarations
603591
let module_build_info = module.build_info();
@@ -1411,42 +1399,20 @@ impl Module for ConcatenatedModule {
14111399
}
14121400

14131401
impl Diagnosable for ConcatenatedModule {
1414-
fn add_diagnostic(&self, diagnostic: Diagnostic) {
1415-
self
1416-
.diagnostics
1417-
.lock()
1418-
.expect("should be able to lock diagnostics")
1419-
.push(diagnostic);
1420-
}
1421-
1422-
fn add_diagnostics(&self, mut diagnostics: Vec<Diagnostic>) {
1423-
self
1424-
.diagnostics
1425-
.lock()
1426-
.expect("should be able to lock diagnostics")
1427-
.append(&mut diagnostics);
1428-
}
1429-
1430-
fn clone_diagnostics(&self) -> Vec<Diagnostic> {
1431-
self
1432-
.diagnostics
1433-
.lock()
1434-
.expect("should be able to lock diagnostics")
1435-
.iter()
1436-
.cloned()
1437-
.collect()
1402+
fn add_diagnostic(&mut self, diagnostic: Diagnostic) {
1403+
self.diagnostics.push(diagnostic);
14381404
}
1439-
}
14401405

1441-
impl ConcatenatedModule {
1442-
fn clear_diagnostics(&mut self) {
1443-
self
1444-
.diagnostics
1445-
.lock()
1446-
.expect("should be able to lock diagnostics")
1447-
.clear()
1406+
fn add_diagnostics(&mut self, mut diagnostics: Vec<Diagnostic>) {
1407+
self.diagnostics.append(&mut diagnostics);
14481408
}
14491409

1410+
fn diagnostics(&self) -> Cow<[Diagnostic]> {
1411+
Cow::Borrowed(&self.diagnostics)
1412+
}
1413+
}
1414+
1415+
impl ConcatenatedModule {
14501416
// TODO: replace self.modules with indexmap or linkedhashset
14511417
fn get_modules_with_info(
14521418
&self,

crates/rspack_core/src/context_module.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rspack_cacheable::{
1010
with::{AsOption, AsPreset, AsVec, Unsupported},
1111
};
1212
use rspack_collections::{Identifiable, Identifier};
13-
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
13+
use rspack_error::{impl_empty_diagnosable_trait, Result};
1414
use rspack_macros::impl_source_map_config;
1515
use rspack_paths::{ArcPath, Utf8PathBuf};
1616
use rspack_regex::RspackRegex;
@@ -847,10 +847,6 @@ impl Module for ContextModule {
847847
&[SourceType::JavaScript]
848848
}
849849

850-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
851-
vec![]
852-
}
853-
854850
fn original_source(&self) -> Option<&dyn rspack_sources::Source> {
855851
None
856852
}

crates/rspack_core/src/external_module.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{borrow::Cow, hash::Hash, iter};
22

33
use rspack_cacheable::{cacheable, cacheable_dyn};
44
use rspack_collections::{Identifiable, Identifier};
5-
use rspack_error::{error, impl_empty_diagnosable_trait, Diagnostic, Result};
5+
use rspack_error::{error, impl_empty_diagnosable_trait, Result};
66
use rspack_hash::RspackHash;
77
use rspack_macros::impl_source_map_config;
88
use rspack_util::{ext::DynHash, json_stringify, source_map::SourceMapKind};
@@ -490,10 +490,6 @@ impl Module for ExternalModule {
490490
&ModuleType::JsAuto
491491
}
492492

493-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
494-
vec![]
495-
}
496-
497493
fn source_types(&self) -> &[SourceType] {
498494
if self.external_type == "css-import" {
499495
EXTERNAL_MODULE_CSS_SOURCE_TYPES

crates/rspack_core/src/module.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rspack_cacheable::{
1111
with::{AsOption, AsVec},
1212
};
1313
use rspack_collections::{Identifiable, Identifier, IdentifierSet};
14-
use rspack_error::{Diagnosable, Diagnostic, Result};
14+
use rspack_error::{Diagnosable, Result};
1515
use rspack_fs::ReadableFileSystem;
1616
use rspack_hash::RspackHashDigest;
1717
use rspack_paths::ArcPath;
@@ -218,7 +218,7 @@ pub trait Module:
218218

219219
/// Defines what kind of code generation results this module can generate.
220220
fn source_types(&self) -> &[SourceType];
221-
fn get_diagnostics(&self) -> Vec<Diagnostic>;
221+
222222
/// The original source of the module. This could be optional, modules like the `NormalModule` can have the corresponding original source.
223223
/// However, modules that is created from "nowhere" (e.g. `ExternalModule` and `MissingModule`) does not have its original source.
224224
fn original_source(&self) -> Option<&dyn Source>;
@@ -606,7 +606,7 @@ mod test {
606606

607607
use rspack_cacheable::cacheable;
608608
use rspack_collections::{Identifiable, Identifier};
609-
use rspack_error::{Diagnosable, Diagnostic, Result};
609+
use rspack_error::{impl_empty_diagnosable_trait, Result};
610610
use rspack_sources::Source;
611611
use rspack_util::source_map::{ModuleSourceMapConfig, SourceMapKind};
612612

@@ -633,7 +633,7 @@ mod test {
633633
}
634634
}
635635

636-
impl Diagnosable for $ident {}
636+
impl_empty_diagnosable_trait!($ident);
637637

638638
impl DependenciesBlock for $ident {
639639
fn add_block_id(&mut self, _: AsyncDependenciesBlockIdentifier) {
@@ -692,10 +692,6 @@ mod test {
692692
unreachable!()
693693
}
694694

695-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
696-
vec![]
697-
}
698-
699695
fn update_hash(
700696
&self,
701697
_hasher: &mut dyn std::hash::Hasher,

crates/rspack_core/src/normal_module.rs

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
ptr::NonNull,
55
sync::{
66
atomic::{AtomicUsize, Ordering},
7-
Arc, Mutex,
7+
Arc,
88
},
99
};
1010

@@ -143,7 +143,7 @@ pub struct NormalModule {
143143
#[cacheable(with=AsMap)]
144144
cached_source_sizes: DashMap<SourceType, f64, BuildHasherDefault<FxHasher>>,
145145
#[cacheable(with=Skip)]
146-
diagnostics: Mutex<Vec<Diagnostic>>,
146+
diagnostics: Vec<Diagnostic>,
147147

148148
code_generation_dependencies: Option<Vec<Box<dyn ModuleDependency>>>,
149149
presentational_dependencies: Option<Vec<Box<dyn DependencyTemplate>>>,
@@ -231,7 +231,7 @@ impl NormalModule {
231231
debug_id: DEBUG_ID.fetch_add(1, Ordering::Relaxed),
232232

233233
cached_source_sizes: DashMap::default(),
234-
diagnostics: Mutex::new(Default::default()),
234+
diagnostics: Default::default(),
235235
code_generation_dependencies: None,
236236
presentational_dependencies: None,
237237
factory_meta: None,
@@ -371,11 +371,6 @@ impl Module for NormalModule {
371371
&self.module_type
372372
}
373373

374-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
375-
let guard = self.diagnostics.lock().expect("should have diagnostics");
376-
guard.clone()
377-
}
378-
379374
fn source_types(&self) -> &[SourceType] {
380375
self.parser_and_generator.source_types()
381376
}
@@ -408,8 +403,6 @@ impl Module for NormalModule {
408403
build_context: BuildContext,
409404
_compilation: Option<&Compilation>,
410405
) -> Result<BuildResult> {
411-
self.clear_diagnostics();
412-
413406
// so does webpack
414407
self.parsed = true;
415408

@@ -545,7 +538,7 @@ impl Module for NormalModule {
545538
if no_parse {
546539
self.parsed = false;
547540
self.original_source = Some(original_source.clone());
548-
self.source = NormalModuleSource::new_built(original_source, self.clone_diagnostics());
541+
self.source = NormalModuleSource::new_built(original_source, self.diagnostics.clone());
549542
self.code_generation_dependencies = Some(Vec::new());
550543
self.presentational_dependencies = Some(Vec::new());
551544

@@ -607,7 +600,7 @@ impl Module for NormalModule {
607600
// Only side effects used in code_generate can stay here
608601
// Other side effects should be set outside use_cache
609602
self.original_source = Some(source.clone());
610-
self.source = NormalModuleSource::new_built(source, self.clone_diagnostics());
603+
self.source = NormalModuleSource::new_built(source, self.diagnostics.clone());
611604
self.code_generation_dependencies = Some(code_generation_dependencies);
612605
self.presentational_dependencies = Some(presentational_dependencies);
613606

@@ -798,30 +791,16 @@ impl Module for NormalModule {
798791
}
799792

800793
impl Diagnosable for NormalModule {
801-
fn add_diagnostic(&self, diagnostic: Diagnostic) {
802-
self
803-
.diagnostics
804-
.lock()
805-
.expect("should be able to lock diagnostics")
806-
.push(diagnostic);
794+
fn add_diagnostic(&mut self, diagnostic: Diagnostic) {
795+
self.diagnostics.push(diagnostic);
807796
}
808797

809-
fn add_diagnostics(&self, mut diagnostics: Vec<Diagnostic>) {
810-
self
811-
.diagnostics
812-
.lock()
813-
.expect("should be able to lock diagnostics")
814-
.append(&mut diagnostics);
798+
fn add_diagnostics(&mut self, mut diagnostics: Vec<Diagnostic>) {
799+
self.diagnostics.append(&mut diagnostics);
815800
}
816801

817-
fn clone_diagnostics(&self) -> Vec<Diagnostic> {
818-
self
819-
.diagnostics
820-
.lock()
821-
.expect("should be able to lock diagnostics")
822-
.iter()
823-
.cloned()
824-
.collect()
802+
fn diagnostics(&self) -> Cow<[Diagnostic]> {
803+
Cow::Borrowed(&self.diagnostics)
825804
}
826805
}
827806

@@ -851,12 +830,4 @@ impl NormalModule {
851830
}
852831
Ok(RawStringSource::from(content.into_string_lossy()).boxed())
853832
}
854-
855-
fn clear_diagnostics(&mut self) {
856-
self
857-
.diagnostics
858-
.lock()
859-
.expect("should be able to lock diagnostics")
860-
.clear()
861-
}
862833
}

crates/rspack_core/src/raw_module.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22

33
use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset};
44
use rspack_collections::Identifiable;
5-
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
5+
use rspack_error::{impl_empty_diagnosable_trait, Result};
66
use rspack_macros::impl_source_map_config;
77
use rspack_sources::{BoxSource, RawStringSource, Source, SourceExt};
88
use rspack_util::source_map::SourceMapKind;
@@ -92,10 +92,6 @@ impl DependenciesBlock for RawModule {
9292
impl Module for RawModule {
9393
impl_module_meta_info!();
9494

95-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
96-
vec![]
97-
}
98-
9995
fn module_type(&self) -> &ModuleType {
10096
&ModuleType::JsAuto
10197
}

crates/rspack_core/src/self_module.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Cow;
33
use async_trait::async_trait;
44
use rspack_cacheable::{cacheable, cacheable_dyn};
55
use rspack_collections::{Identifiable, Identifier};
6-
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
6+
use rspack_error::{impl_empty_diagnosable_trait, Result};
77
use rspack_macros::impl_source_map_config;
88
use rspack_sources::Source;
99
use rspack_util::source_map::SourceMapKind;
@@ -79,10 +79,6 @@ impl DependenciesBlock for SelfModule {
7979
impl Module for SelfModule {
8080
impl_module_meta_info!();
8181

82-
fn get_diagnostics(&self) -> Vec<Diagnostic> {
83-
vec![]
84-
}
85-
8682
fn size(&self, _source_type: Option<&SourceType>, _compilation: Option<&Compilation>) -> f64 {
8783
self.identifier.len() as f64
8884
}

0 commit comments

Comments
 (0)