Skip to content

Commit 0460c23

Browse files
giacomocavalierilpil
authored andcommitted
make sure we can test warnings even in the presence of errors
1 parent 3be7857 commit 0460c23

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

compiler-core/src/type_/tests.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ fn get_warnings(
191191
target,
192192
TargetSupport::NotEnforced,
193193
gleam_version,
194-
)
195-
.expect("Compilation should succeed");
194+
);
196195
warnings.take().into_iter().collect_vec()
197196
}
198197

@@ -455,7 +454,7 @@ pub fn compile_module(
455454
src: &str,
456455
warnings: Option<Rc<dyn WarningEmitterIO>>,
457456
dep: Vec<DependencyModule<'_>>,
458-
) -> Result<TypedModule, (Vec<crate::type_::Error>, Names)> {
457+
) -> Outcome<TypedModule, Vec1<super::Error>> {
459458
compile_module_with_opts(
460459
module_name,
461460
src,
@@ -475,7 +474,7 @@ pub fn compile_module_with_opts(
475474
target: Target,
476475
target_support: TargetSupport,
477476
gleam_version: Option<Range<Version>>,
478-
) -> Result<TypedModule, (Vec<crate::type_::Error>, Names)> {
477+
) -> Outcome<TypedModule, Vec1<super::Error>> {
479478
let ids = UniqueIdGenerator::new();
480479
let mut modules = im::HashMap::new();
481480

@@ -527,7 +526,7 @@ pub fn compile_module_with_opts(
527526
config.gleam_version = gleam_version.map(|v| GleamVersion::from_pubgrub(v));
528527

529528
let warnings = TypeWarningEmitter::new("/src/warning/wrn.gleam".into(), src.into(), emitter);
530-
let inference_result = crate::analyse::ModuleAnalyzerConstructor::<()> {
529+
crate::analyse::ModuleAnalyzerConstructor::<()> {
531530
target,
532531
ids: &ids,
533532
origin: Origin::Src,
@@ -538,13 +537,7 @@ pub fn compile_module_with_opts(
538537
target_support: TargetSupport::Enforced,
539538
package_config: &config,
540539
}
541-
.infer_module(ast, LineNumbers::new(src), "".into());
542-
543-
match inference_result {
544-
Outcome::Ok(ast) => Ok(ast),
545-
Outcome::PartialFailure(ast, errors) => Err((errors.into(), ast.names)),
546-
Outcome::TotalFailure(error) => Err((error.into(), Default::default())),
547-
}
540+
.infer_module(ast, LineNumbers::new(src), "".into())
548541
}
549542

550543
pub fn module_error(src: &str, deps: Vec<DependencyModule<'_>>) -> String {
@@ -556,16 +549,22 @@ pub fn module_error_with_target(
556549
deps: Vec<DependencyModule<'_>>,
557550
target: Target,
558551
) -> String {
559-
let (error, names) = compile_module_with_opts(
552+
let outcome = compile_module_with_opts(
560553
"themodule",
561554
src,
562555
None,
563556
deps,
564557
target,
565558
TargetSupport::NotEnforced,
566559
None,
567-
)
568-
.expect_err("should infer an error");
560+
);
561+
562+
let (error, names) = match outcome {
563+
Outcome::Ok(_) => panic!("should infer an error"),
564+
Outcome::PartialFailure(ast, errors) => (errors.into(), ast.names),
565+
Outcome::TotalFailure(errors) => (errors.into(), Default::default()),
566+
};
567+
569568
let error = Error::Type {
570569
names: Box::new(names),
571570
src: src.into(),
@@ -584,16 +583,22 @@ pub fn internal_module_error_with_target(
584583
deps: Vec<DependencyModule<'_>>,
585584
target: Target,
586585
) -> String {
587-
let (error, names) = compile_module_with_opts(
586+
let outcome = compile_module_with_opts(
588587
"thepackage/internal/themodule",
589588
src,
590589
None,
591590
deps,
592591
target,
593592
TargetSupport::NotEnforced,
594593
None,
595-
)
596-
.expect_err("should infer an error");
594+
);
595+
596+
let (error, names) = match outcome {
597+
Outcome::Ok(_) => panic!("should infer an error"),
598+
Outcome::PartialFailure(ast, errors) => (errors.into(), ast.names),
599+
Outcome::TotalFailure(errors) => (errors.into(), Default::default()),
600+
};
601+
597602
let error = Error::Type {
598603
names: Box::new(names),
599604
src: src.into(),

compiler-core/src/type_/tests/target_implementations.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ pub fn no_valid_erlang_impl() {
323323
TargetSupport::Enforced,
324324
None,
325325
);
326-
assert!(out.is_err());
326+
assert!(out.into_result().is_err());
327327
}
328328

329329
#[test]
@@ -350,7 +350,7 @@ pub fn no_valid_javascript_impl() {
350350
TargetSupport::Enforced,
351351
None,
352352
);
353-
assert!(out.is_err());
353+
assert!(out.into_result().is_err());
354354
}
355355

356356
#[test]
@@ -377,7 +377,7 @@ pub fn no_valid_erlang_impl() {
377377
TargetSupport::Enforced,
378378
None,
379379
);
380-
assert!(out.is_err());
380+
assert!(out.into_result().is_err());
381381
}
382382

383383
#[test]
@@ -404,7 +404,7 @@ pub fn no_valid_javascript_impl() {
404404
TargetSupport::Enforced,
405405
None,
406406
);
407-
assert!(out.is_err());
407+
assert!(out.into_result().is_err());
408408
}
409409

410410
#[test]
@@ -427,7 +427,7 @@ pub fn no_valid_erlang_impl() {
427427
TargetSupport::Enforced,
428428
None,
429429
);
430-
assert!(out.is_err());
430+
assert!(out.into_result().is_err());
431431
}
432432

433433
#[test]
@@ -450,5 +450,5 @@ pub fn no_valid_javascript_impl() {
450450
TargetSupport::Enforced,
451451
None,
452452
);
453-
assert!(out.is_err());
453+
assert!(out.into_result().is_err());
454454
}

0 commit comments

Comments
 (0)