2023-06-14 22:17:22 +00:00
|
|
|
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
|
2021-06-13 16:51:19 +00:00
|
|
|
|
|
|
|
// Diagnostic: break-outside-of-loop
|
|
|
|
//
|
|
|
|
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
|
2021-06-14 16:32:39 +00:00
|
|
|
pub(crate) fn break_outside_of_loop(
|
2021-06-13 16:51:19 +00:00
|
|
|
ctx: &DiagnosticsContext<'_>,
|
|
|
|
d: &hir::BreakOutsideOfLoop,
|
|
|
|
) -> Diagnostic {
|
2023-03-03 16:28:57 +00:00
|
|
|
let message = if d.bad_value_break {
|
|
|
|
"can't break with a value in this position".to_owned()
|
|
|
|
} else {
|
|
|
|
let construct = if d.is_break { "break" } else { "continue" };
|
|
|
|
format!("{construct} outside of loop")
|
|
|
|
};
|
2023-06-14 22:17:22 +00:00
|
|
|
Diagnostic::new_with_syntax_node_ptr(
|
|
|
|
ctx,
|
|
|
|
DiagnosticCode::RustcHardError("E0268"),
|
2023-03-03 16:28:57 +00:00
|
|
|
message,
|
2024-01-18 12:59:49 +00:00
|
|
|
d.expr.map(|it| it.into()),
|
2021-06-13 16:51:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-14 10:15:05 +00:00
|
|
|
use crate::tests::check_diagnostics;
|
2021-06-13 16:51:19 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-09-01 12:30:57 +00:00
|
|
|
fn outside_of_loop() {
|
2021-06-13 16:51:19 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
2022-09-01 12:30:57 +00:00
|
|
|
fn foo() {
|
|
|
|
break;
|
|
|
|
//^^^^^ error: break outside of loop
|
|
|
|
continue;
|
|
|
|
//^^^^^^^^ error: continue outside of loop
|
|
|
|
}
|
2022-09-01 12:54:47 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-28 20:46:45 +00:00
|
|
|
fn async_blocks_are_borders() {
|
2022-09-01 12:54:47 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
2022-12-28 20:53:55 +00:00
|
|
|
async {
|
2022-09-01 12:54:47 +00:00
|
|
|
break;
|
|
|
|
//^^^^^ error: break outside of loop
|
|
|
|
continue;
|
|
|
|
//^^^^^^^^ error: continue outside of loop
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-28 20:46:45 +00:00
|
|
|
fn closures_are_borders() {
|
2022-09-01 12:54:47 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
2022-12-28 20:54:38 +00:00
|
|
|
|| {
|
2022-09-01 12:54:47 +00:00
|
|
|
break;
|
|
|
|
//^^^^^ error: break outside of loop
|
|
|
|
continue;
|
|
|
|
//^^^^^^^^ error: continue outside of loop
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-28 20:46:45 +00:00
|
|
|
fn blocks_pass_through() {
|
2022-09-01 12:54:47 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
2022-12-28 20:46:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-01 12:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-28 20:46:45 +00:00
|
|
|
fn try_blocks_pass_through() {
|
2022-09-01 12:54:47 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
2022-12-28 20:46:45 +00:00
|
|
|
try {
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
};
|
2022-09-01 12:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn label_blocks() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: {
|
|
|
|
break;
|
|
|
|
//^^^^^ error: break outside of loop
|
|
|
|
continue;
|
|
|
|
//^^^^^^^^ error: continue outside of loop
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 16:28:57 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn value_break_in_for_loop() {
|
2023-05-25 21:15:37 +00:00
|
|
|
// FIXME: the error is correct, but the message is terrible
|
2023-03-03 16:28:57 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
2023-05-25 21:15:37 +00:00
|
|
|
//- minicore: iterator
|
2023-03-03 16:28:57 +00:00
|
|
|
fn test() {
|
|
|
|
for _ in [()] {
|
|
|
|
break 3;
|
2023-05-25 21:15:37 +00:00
|
|
|
// ^ error: expected (), found i32
|
2023-03-03 16:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-01 22:58:14 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn try_block_desugaring_inside_closure() {
|
|
|
|
// regression test for #14701
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
//- minicore: option, try
|
|
|
|
fn test() {
|
|
|
|
try {
|
|
|
|
|| {
|
|
|
|
let x = Some(2);
|
|
|
|
Some(x?)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2021-06-13 16:51:19 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|