mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-21 04:03:11 +00:00
Merge #9256
9256: internal: kill diagnostic sink r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
8c5c0ef7b9
@ -3,18 +3,12 @@
|
||||
//!
|
||||
//! This probably isn't the best way to do this -- ideally, diagnistics should
|
||||
//! be expressed in terms of hir types themselves.
|
||||
use std::any::Any;
|
||||
|
||||
use cfg::{CfgExpr, CfgOptions};
|
||||
use either::Either;
|
||||
use hir_def::path::ModPath;
|
||||
use hir_expand::{name::Name, HirFileId, InFile};
|
||||
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
|
||||
|
||||
pub use crate::diagnostics_sink::{
|
||||
Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder,
|
||||
};
|
||||
|
||||
macro_rules! diagnostics {
|
||||
($($diag:ident,)*) => {
|
||||
pub enum AnyDiagnostic {$(
|
||||
@ -38,6 +32,7 @@ diagnostics![
|
||||
MacroError,
|
||||
MismatchedArgCount,
|
||||
MissingFields,
|
||||
MissingMatchArms,
|
||||
MissingOkOrSomeInTailExpr,
|
||||
MissingUnsafe,
|
||||
NoSuchField,
|
||||
@ -149,9 +144,6 @@ pub struct MissingOkOrSomeInTailExpr {
|
||||
pub required: String,
|
||||
}
|
||||
|
||||
// Diagnostic: missing-match-arm
|
||||
//
|
||||
// This diagnostic is triggered if `match` block is missing one or more match arms.
|
||||
#[derive(Debug)]
|
||||
pub struct MissingMatchArms {
|
||||
pub file: HirFileId,
|
||||
@ -159,40 +151,4 @@ pub struct MissingMatchArms {
|
||||
pub arms: AstPtr<ast::MatchArmList>,
|
||||
}
|
||||
|
||||
impl Diagnostic for MissingMatchArms {
|
||||
fn code(&self) -> DiagnosticCode {
|
||||
DiagnosticCode("missing-match-arm")
|
||||
}
|
||||
fn message(&self) -> String {
|
||||
String::from("Missing match arm")
|
||||
}
|
||||
fn display_source(&self) -> InFile<SyntaxNodePtr> {
|
||||
InFile { file_id: self.file, value: self.match_expr.clone().into() }
|
||||
}
|
||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InternalBailedOut {
|
||||
pub file: HirFileId,
|
||||
pub pat_syntax_ptr: SyntaxNodePtr,
|
||||
}
|
||||
|
||||
impl Diagnostic for InternalBailedOut {
|
||||
fn code(&self) -> DiagnosticCode {
|
||||
DiagnosticCode("internal:match-check-bailed-out")
|
||||
}
|
||||
fn message(&self) -> String {
|
||||
format!("Internal: match check bailed out")
|
||||
}
|
||||
fn display_source(&self) -> InFile<SyntaxNodePtr> {
|
||||
InFile { file_id: self.file, value: self.pat_syntax_ptr.clone() }
|
||||
}
|
||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub use hir_ty::diagnostics::IncorrectCase;
|
||||
|
@ -1,109 +0,0 @@
|
||||
//! Semantic errors and warnings.
|
||||
//!
|
||||
//! The `Diagnostic` trait defines a trait object which can represent any
|
||||
//! diagnostic.
|
||||
//!
|
||||
//! `DiagnosticSink` struct is used as an emitter for diagnostic. When creating
|
||||
//! a `DiagnosticSink`, you supply a callback which can react to a `dyn
|
||||
//! Diagnostic` or to any concrete diagnostic (downcasting is used internally).
|
||||
//!
|
||||
//! Because diagnostics store file offsets, it's a bad idea to store them
|
||||
//! directly in salsa. For this reason, every hir subsytem defines it's own
|
||||
//! strongly-typed closed set of diagnostics which use hir ids internally, are
|
||||
//! stored in salsa and do *not* implement the `Diagnostic` trait. Instead, a
|
||||
//! subsystem provides a separate, non-query-based API which can walk all stored
|
||||
//! values and transform them into instances of `Diagnostic`.
|
||||
|
||||
use std::{any::Any, fmt};
|
||||
|
||||
use hir_expand::InFile;
|
||||
use syntax::SyntaxNodePtr;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct DiagnosticCode(pub &'static str);
|
||||
|
||||
impl DiagnosticCode {
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
|
||||
fn code(&self) -> DiagnosticCode;
|
||||
fn message(&self) -> String;
|
||||
/// Source element that triggered the diagnostics.
|
||||
///
|
||||
/// Note that this should reflect "semantics", rather than specific span we
|
||||
/// want to highlight. When rendering the diagnostics into an error message,
|
||||
/// the IDE will fetch the `SyntaxNode` and will narrow the span
|
||||
/// appropriately.
|
||||
fn display_source(&self) -> InFile<SyntaxNodePtr>;
|
||||
fn as_any(&self) -> &(dyn Any + Send + 'static);
|
||||
fn is_experimental(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DiagnosticSink<'a> {
|
||||
callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
|
||||
filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
|
||||
default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>,
|
||||
}
|
||||
|
||||
impl<'a> DiagnosticSink<'a> {
|
||||
pub fn push(&mut self, d: impl Diagnostic) {
|
||||
let d: &dyn Diagnostic = &d;
|
||||
self._push(d);
|
||||
}
|
||||
|
||||
fn _push(&mut self, d: &dyn Diagnostic) {
|
||||
for filter in &mut self.filters {
|
||||
if !filter(d) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
for cb in &mut self.callbacks {
|
||||
match cb(d) {
|
||||
Ok(()) => return,
|
||||
Err(()) => (),
|
||||
}
|
||||
}
|
||||
(self.default_callback)(d)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DiagnosticSinkBuilder<'a> {
|
||||
callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
|
||||
filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
|
||||
}
|
||||
|
||||
impl<'a> DiagnosticSinkBuilder<'a> {
|
||||
pub fn new() -> Self {
|
||||
Self { callbacks: Vec::new(), filters: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self {
|
||||
self.filters.push(Box::new(cb));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self {
|
||||
let cb = move |diag: &dyn Diagnostic| match diag.as_any().downcast_ref::<D>() {
|
||||
Some(d) => {
|
||||
cb(d);
|
||||
Ok(())
|
||||
}
|
||||
None => Err(()),
|
||||
};
|
||||
self.callbacks.push(Box::new(cb));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> {
|
||||
DiagnosticSink {
|
||||
callbacks: self.callbacks,
|
||||
filters: self.filters,
|
||||
default_callback: Box::new(default_callback),
|
||||
}
|
||||
}
|
||||
}
|
@ -27,7 +27,6 @@ mod attrs;
|
||||
mod has_source;
|
||||
|
||||
pub mod diagnostics;
|
||||
pub mod diagnostics_sink;
|
||||
pub mod db;
|
||||
|
||||
mod display;
|
||||
@ -78,16 +77,13 @@ use syntax::{
|
||||
};
|
||||
use tt::{Ident, Leaf, Literal, TokenTree};
|
||||
|
||||
use crate::{
|
||||
db::{DefDatabase, HirDatabase},
|
||||
diagnostics_sink::DiagnosticSink,
|
||||
};
|
||||
use crate::db::{DefDatabase, HirDatabase};
|
||||
|
||||
pub use crate::{
|
||||
attrs::{HasAttrs, Namespace},
|
||||
diagnostics::{
|
||||
AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase, InternalBailedOut,
|
||||
MacroError, MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr,
|
||||
AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase, MacroError,
|
||||
MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr,
|
||||
MissingUnsafe, NoSuchField, RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap,
|
||||
UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall,
|
||||
UnresolvedModule, UnresolvedProcMacro,
|
||||
@ -457,16 +453,10 @@ impl Module {
|
||||
self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of((*def).into())
|
||||
}
|
||||
|
||||
pub fn diagnostics(
|
||||
self,
|
||||
db: &dyn HirDatabase,
|
||||
sink: &mut DiagnosticSink,
|
||||
internal_diagnostics: bool,
|
||||
) -> Vec<AnyDiagnostic> {
|
||||
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
|
||||
let _p = profile::span("Module::diagnostics").detail(|| {
|
||||
format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
|
||||
});
|
||||
let mut acc: Vec<AnyDiagnostic> = Vec::new();
|
||||
let def_map = self.id.def_map(db.upcast());
|
||||
for diag in def_map.diagnostics() {
|
||||
if diag.in_module != self.id.local_id {
|
||||
@ -619,11 +609,11 @@ impl Module {
|
||||
}
|
||||
for decl in self.declarations(db) {
|
||||
match decl {
|
||||
ModuleDef::Function(f) => acc.extend(f.diagnostics(db, sink, internal_diagnostics)),
|
||||
ModuleDef::Function(f) => f.diagnostics(db, acc),
|
||||
ModuleDef::Module(m) => {
|
||||
// Only add diagnostics from inline modules
|
||||
if def_map[m.id.local_id].origin.is_inline() {
|
||||
acc.extend(m.diagnostics(db, sink, internal_diagnostics))
|
||||
m.diagnostics(db, acc)
|
||||
}
|
||||
}
|
||||
_ => acc.extend(decl.diagnostics(db)),
|
||||
@ -633,11 +623,10 @@ impl Module {
|
||||
for impl_def in self.impl_defs(db) {
|
||||
for item in impl_def.items(db) {
|
||||
if let AssocItem::Function(f) = item {
|
||||
acc.extend(f.diagnostics(db, sink, internal_diagnostics));
|
||||
f.diagnostics(db, acc);
|
||||
}
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
|
||||
@ -1036,13 +1025,7 @@ impl Function {
|
||||
db.function_data(self.id).is_async()
|
||||
}
|
||||
|
||||
pub fn diagnostics(
|
||||
self,
|
||||
db: &dyn HirDatabase,
|
||||
sink: &mut DiagnosticSink,
|
||||
internal_diagnostics: bool,
|
||||
) -> Vec<AnyDiagnostic> {
|
||||
let mut acc: Vec<AnyDiagnostic> = Vec::new();
|
||||
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
|
||||
let krate = self.module(db).id.krate();
|
||||
|
||||
let source_map = db.body_with_source_map(self.id.into()).1;
|
||||
@ -1100,9 +1083,7 @@ impl Function {
|
||||
}
|
||||
}
|
||||
|
||||
for diagnostic in
|
||||
BodyValidationDiagnostic::collect(db, self.id.into(), internal_diagnostics)
|
||||
{
|
||||
for diagnostic in BodyValidationDiagnostic::collect(db, self.id.into()) {
|
||||
match diagnostic {
|
||||
BodyValidationDiagnostic::RecordMissingFields {
|
||||
record,
|
||||
@ -1209,36 +1190,26 @@ impl Function {
|
||||
if let (Some(match_expr), Some(arms)) =
|
||||
(match_expr.expr(), match_expr.match_arm_list())
|
||||
{
|
||||
sink.push(MissingMatchArms {
|
||||
file: source_ptr.file_id,
|
||||
match_expr: AstPtr::new(&match_expr),
|
||||
arms: AstPtr::new(&arms),
|
||||
})
|
||||
acc.push(
|
||||
MissingMatchArms {
|
||||
file: source_ptr.file_id,
|
||||
match_expr: AstPtr::new(&match_expr),
|
||||
arms: AstPtr::new(&arms),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(SyntheticSyntax) => (),
|
||||
}
|
||||
}
|
||||
BodyValidationDiagnostic::InternalBailedOut { pat } => {
|
||||
match source_map.pat_syntax(pat) {
|
||||
Ok(source_ptr) => {
|
||||
let pat_syntax_ptr = source_ptr.value.either(Into::into, Into::into);
|
||||
sink.push(InternalBailedOut {
|
||||
file: source_ptr.file_id,
|
||||
pat_syntax_ptr,
|
||||
});
|
||||
}
|
||||
Err(SyntheticSyntax) => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) {
|
||||
acc.push(diag.into())
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
/// Whether this function declaration has a definition.
|
||||
|
@ -50,21 +50,13 @@ pub enum BodyValidationDiagnostic {
|
||||
MissingMatchArms {
|
||||
match_expr: ExprId,
|
||||
},
|
||||
InternalBailedOut {
|
||||
pat: PatId,
|
||||
},
|
||||
}
|
||||
|
||||
impl BodyValidationDiagnostic {
|
||||
pub fn collect(
|
||||
db: &dyn HirDatabase,
|
||||
owner: DefWithBodyId,
|
||||
internal_diagnostics: bool,
|
||||
) -> Vec<BodyValidationDiagnostic> {
|
||||
pub fn collect(db: &dyn HirDatabase, owner: DefWithBodyId) -> Vec<BodyValidationDiagnostic> {
|
||||
let _p = profile::span("BodyValidationDiagnostic::collect");
|
||||
let infer = db.infer(owner);
|
||||
let mut validator = ExprValidator::new(owner, infer.clone());
|
||||
validator.internal_diagnostics = internal_diagnostics;
|
||||
validator.validate_body(db);
|
||||
validator.diagnostics
|
||||
}
|
||||
@ -74,12 +66,11 @@ struct ExprValidator {
|
||||
owner: DefWithBodyId,
|
||||
infer: Arc<InferenceResult>,
|
||||
pub(super) diagnostics: Vec<BodyValidationDiagnostic>,
|
||||
internal_diagnostics: bool,
|
||||
}
|
||||
|
||||
impl ExprValidator {
|
||||
fn new(owner: DefWithBodyId, infer: Arc<InferenceResult>) -> ExprValidator {
|
||||
ExprValidator { owner, infer, diagnostics: Vec::new(), internal_diagnostics: false }
|
||||
ExprValidator { owner, infer, diagnostics: Vec::new() }
|
||||
}
|
||||
|
||||
fn validate_body(&mut self, db: &dyn HirDatabase) {
|
||||
@ -308,9 +299,7 @@ impl ExprValidator {
|
||||
// fit the match expression, we skip this diagnostic. Skipping the entire
|
||||
// diagnostic rather than just not including this match arm is preferred
|
||||
// to avoid the chance of false positives.
|
||||
if self.internal_diagnostics {
|
||||
self.diagnostics.push(BodyValidationDiagnostic::InternalBailedOut { pat: arm.pat })
|
||||
}
|
||||
cov_mark::hit!(validate_match_bailed_out);
|
||||
return;
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
929
crates/ide/src/diagnostics/missing_match_arms.rs
Normal file
929
crates/ide/src/diagnostics/missing_match_arms.rs
Normal file
@ -0,0 +1,929 @@
|
||||
use hir::InFile;
|
||||
|
||||
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
|
||||
|
||||
// Diagnostic: missing-match-arm
|
||||
//
|
||||
// This diagnostic is triggered if `match` block is missing one or more match arms.
|
||||
pub(super) fn missing_match_arms(
|
||||
ctx: &DiagnosticsContext<'_>,
|
||||
d: &hir::MissingMatchArms,
|
||||
) -> Diagnostic {
|
||||
Diagnostic::new(
|
||||
"missing-match-arm",
|
||||
"missing match arm",
|
||||
ctx.sema.diagnostics_display_range(InFile::new(d.file, d.match_expr.clone().into())).range,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) mod tests {
|
||||
use crate::diagnostics::tests::check_diagnostics;
|
||||
|
||||
fn check_diagnostics_no_bails(ra_fixture: &str) {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 0);
|
||||
crate::diagnostics::tests::check_diagnostics(ra_fixture)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_tuple() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match () { }
|
||||
//^^ missing match arm
|
||||
match (()) { }
|
||||
//^^^^ missing match arm
|
||||
|
||||
match () { _ => (), }
|
||||
match () { () => (), }
|
||||
match (()) { (()) => (), }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_of_two_empty_tuple() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match ((), ()) { }
|
||||
//^^^^^^^^ missing match arm
|
||||
|
||||
match ((), ()) { ((), ()) => (), }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn test_main() {
|
||||
match false { }
|
||||
//^^^^^ missing match arm
|
||||
match false { true => (), }
|
||||
//^^^^^ missing match arm
|
||||
match (false, true) {}
|
||||
//^^^^^^^^^^^^^ missing match arm
|
||||
match (false, true) { (true, true) => (), }
|
||||
//^^^^^^^^^^^^^ missing match arm
|
||||
match (false, true) {
|
||||
//^^^^^^^^^^^^^ missing match arm
|
||||
(false, true) => (),
|
||||
(false, false) => (),
|
||||
(true, false) => (),
|
||||
}
|
||||
match (false, true) { (true, _x) => (), }
|
||||
//^^^^^^^^^^^^^ missing match arm
|
||||
|
||||
match false { true => (), false => (), }
|
||||
match (false, true) {
|
||||
(false, _) => (),
|
||||
(true, false) => (),
|
||||
(_, true) => (),
|
||||
}
|
||||
match (false, true) {
|
||||
(true, true) => (),
|
||||
(true, false) => (),
|
||||
(false, true) => (),
|
||||
(false, false) => (),
|
||||
}
|
||||
match (false, true) {
|
||||
(true, _x) => (),
|
||||
(false, true) => (),
|
||||
(false, false) => (),
|
||||
}
|
||||
match (false, true, false) {
|
||||
(false, ..) => (),
|
||||
(true, ..) => (),
|
||||
}
|
||||
match (false, true, false) {
|
||||
(.., false) => (),
|
||||
(.., true) => (),
|
||||
}
|
||||
match (false, true, false) { (..) => (), }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_of_tuple_and_bools() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match (false, ((), false)) {}
|
||||
//^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
match (false, ((), false)) { (true, ((), true)) => (), }
|
||||
//^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
match (false, ((), false)) { (true, _) => (), }
|
||||
//^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
|
||||
match (false, ((), false)) {
|
||||
(true, ((), true)) => (),
|
||||
(true, ((), false)) => (),
|
||||
(false, ((), true)) => (),
|
||||
(false, ((), false)) => (),
|
||||
}
|
||||
match (false, ((), false)) {
|
||||
(true, ((), true)) => (),
|
||||
(true, ((), false)) => (),
|
||||
(false, _) => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enums() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either { A, B, }
|
||||
|
||||
fn main() {
|
||||
match Either::A { }
|
||||
//^^^^^^^^^ missing match arm
|
||||
match Either::B { Either::A => (), }
|
||||
//^^^^^^^^^ missing match arm
|
||||
|
||||
match &Either::B {
|
||||
//^^^^^^^^^^ missing match arm
|
||||
Either::A => (),
|
||||
}
|
||||
|
||||
match Either::B {
|
||||
Either::A => (), Either::B => (),
|
||||
}
|
||||
match &Either::B {
|
||||
Either::A => (), Either::B => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_containing_bool() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either { A(bool), B }
|
||||
|
||||
fn main() {
|
||||
match Either::B { }
|
||||
//^^^^^^^^^ missing match arm
|
||||
match Either::B {
|
||||
//^^^^^^^^^ missing match arm
|
||||
Either::A(true) => (), Either::B => ()
|
||||
}
|
||||
|
||||
match Either::B {
|
||||
Either::A(true) => (),
|
||||
Either::A(false) => (),
|
||||
Either::B => (),
|
||||
}
|
||||
match Either::B {
|
||||
Either::B => (),
|
||||
_ => (),
|
||||
}
|
||||
match Either::B {
|
||||
Either::A(_) => (),
|
||||
Either::B => (),
|
||||
}
|
||||
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_different_sizes() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either { A(bool), B(bool, bool) }
|
||||
|
||||
fn main() {
|
||||
match Either::A(false) {
|
||||
//^^^^^^^^^^^^^^^^ missing match arm
|
||||
Either::A(_) => (),
|
||||
Either::B(false, _) => (),
|
||||
}
|
||||
|
||||
match Either::A(false) {
|
||||
Either::A(_) => (),
|
||||
Either::B(true, _) => (),
|
||||
Either::B(false, _) => (),
|
||||
}
|
||||
match Either::A(false) {
|
||||
Either::A(true) | Either::A(false) => (),
|
||||
Either::B(true, _) => (),
|
||||
Either::B(false, _) => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_of_enum_no_diagnostic() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either { A(bool), B(bool, bool) }
|
||||
enum Either2 { C, D }
|
||||
|
||||
fn main() {
|
||||
match (Either::A(false), Either2::C) {
|
||||
(Either::A(true), _) | (Either::A(false), _) => (),
|
||||
(Either::B(true, _), Either2::C) => (),
|
||||
(Either::B(false, _), Either2::C) => (),
|
||||
(Either::B(_, _), Either2::D) => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn or_pattern_no_diagnostic() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either {A, B}
|
||||
|
||||
fn main() {
|
||||
match (Either::A, Either::B) {
|
||||
(Either::A | Either::B, _) => (),
|
||||
}
|
||||
}"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_types() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 4);
|
||||
// Match statements with arms that don't match the
|
||||
// expression pattern do not fire this diagnostic.
|
||||
check_diagnostics(
|
||||
r#"
|
||||
enum Either { A, B }
|
||||
enum Either2 { C, D }
|
||||
|
||||
fn main() {
|
||||
match Either::A {
|
||||
Either2::C => (),
|
||||
Either2::D => (),
|
||||
}
|
||||
match (true, false) {
|
||||
(true, false, true) => (),
|
||||
(true) => (),
|
||||
}
|
||||
match (true, false) { (true,) => {} }
|
||||
match (0) { () => () }
|
||||
match Unresolved::Bar { Unresolved::Baz => () }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_types_in_or_patterns() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 2);
|
||||
check_diagnostics(
|
||||
r#"
|
||||
fn main() {
|
||||
match false { true | () => {} }
|
||||
match (false,) { (true | (),) => {} }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_match_arm_tuple_enum_missing_pattern() {
|
||||
// We are testing to be sure we don't panic here when the match
|
||||
// arm `Either::B` is missing its pattern.
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either { A, B(u32) }
|
||||
|
||||
fn main() {
|
||||
match Either::A {
|
||||
Either::A => (),
|
||||
Either::B() => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_match_arm_extra_fields() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 2);
|
||||
check_diagnostics(
|
||||
r#"
|
||||
enum A { B(isize, isize), C }
|
||||
fn main() {
|
||||
match A::B(1, 2) {
|
||||
A::B(_, _, _) => (),
|
||||
}
|
||||
match A::B(1, 2) {
|
||||
A::C(_) => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expr_diverges() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 2);
|
||||
check_diagnostics(
|
||||
r#"
|
||||
enum Either { A, B }
|
||||
|
||||
fn main() {
|
||||
match loop {} {
|
||||
Either::A => (),
|
||||
Either::B => (),
|
||||
}
|
||||
match loop {} {
|
||||
Either::A => (),
|
||||
}
|
||||
match loop { break Foo::A } {
|
||||
//^^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
Either::A => (),
|
||||
}
|
||||
match loop { break Foo::A } {
|
||||
Either::A => (),
|
||||
Either::B => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expr_partially_diverges() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either<T> { A(T), B }
|
||||
|
||||
fn foo() -> Either<!> { Either::B }
|
||||
fn main() -> u32 {
|
||||
match foo() {
|
||||
Either::A(val) => val,
|
||||
Either::B => 0,
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_record() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either { A { foo: bool }, B }
|
||||
|
||||
fn main() {
|
||||
let a = Either::A { foo: true };
|
||||
match a { }
|
||||
//^ missing match arm
|
||||
match a { Either::A { foo: true } => () }
|
||||
//^ missing match arm
|
||||
match a {
|
||||
Either::A { } => (),
|
||||
//^^^^^^^^^ Missing structure fields:
|
||||
// | - foo
|
||||
Either::B => (),
|
||||
}
|
||||
match a {
|
||||
//^ missing match arm
|
||||
Either::A { } => (),
|
||||
} //^^^^^^^^^ Missing structure fields:
|
||||
// | - foo
|
||||
|
||||
match a {
|
||||
Either::A { foo: true } => (),
|
||||
Either::A { foo: false } => (),
|
||||
Either::B => (),
|
||||
}
|
||||
match a {
|
||||
Either::A { foo: _ } => (),
|
||||
Either::B => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_record_fields_out_of_order() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either {
|
||||
A { foo: bool, bar: () },
|
||||
B,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Either::A { foo: true, bar: () };
|
||||
match a {
|
||||
//^ missing match arm
|
||||
Either::A { bar: (), foo: false } => (),
|
||||
Either::A { foo: true, bar: () } => (),
|
||||
}
|
||||
|
||||
match a {
|
||||
Either::A { bar: (), foo: false } => (),
|
||||
Either::A { foo: true, bar: () } => (),
|
||||
Either::B => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_record_ellipsis() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either {
|
||||
A { foo: bool, bar: bool },
|
||||
B,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Either::B;
|
||||
match a {
|
||||
//^ missing match arm
|
||||
Either::A { foo: true, .. } => (),
|
||||
Either::B => (),
|
||||
}
|
||||
match a {
|
||||
//^ missing match arm
|
||||
Either::A { .. } => (),
|
||||
}
|
||||
|
||||
match a {
|
||||
Either::A { foo: true, .. } => (),
|
||||
Either::A { foo: false, .. } => (),
|
||||
Either::B => (),
|
||||
}
|
||||
|
||||
match a {
|
||||
Either::A { .. } => (),
|
||||
Either::B => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_tuple_partial_ellipsis() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Either {
|
||||
A(bool, bool, bool, bool),
|
||||
B,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match Either::B {
|
||||
//^^^^^^^^^ missing match arm
|
||||
Either::A(true, .., true) => (),
|
||||
Either::A(true, .., false) => (),
|
||||
Either::A(false, .., false) => (),
|
||||
Either::B => (),
|
||||
}
|
||||
match Either::B {
|
||||
//^^^^^^^^^ missing match arm
|
||||
Either::A(true, .., true) => (),
|
||||
Either::A(true, .., false) => (),
|
||||
Either::A(.., true) => (),
|
||||
Either::B => (),
|
||||
}
|
||||
|
||||
match Either::B {
|
||||
Either::A(true, .., true) => (),
|
||||
Either::A(true, .., false) => (),
|
||||
Either::A(false, .., true) => (),
|
||||
Either::A(false, .., false) => (),
|
||||
Either::B => (),
|
||||
}
|
||||
match Either::B {
|
||||
Either::A(true, .., true) => (),
|
||||
Either::A(true, .., false) => (),
|
||||
Either::A(.., true) => (),
|
||||
Either::A(.., false) => (),
|
||||
Either::B => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn never() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Never {}
|
||||
|
||||
fn enum_(never: Never) {
|
||||
match never {}
|
||||
}
|
||||
fn enum_ref(never: &Never) {
|
||||
match never {}
|
||||
//^^^^^ missing match arm
|
||||
}
|
||||
fn bang(never: !) {
|
||||
match never {}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_type() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 1);
|
||||
|
||||
check_diagnostics(
|
||||
r#"
|
||||
enum Option<T> { Some(T), None }
|
||||
|
||||
fn main() {
|
||||
// `Never` is deliberately not defined so that it's an uninferred type.
|
||||
match Option::<Never>::None {
|
||||
None => (),
|
||||
Some(never) => match never {},
|
||||
}
|
||||
match Option::<Never>::None {
|
||||
//^^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
Option::Some(_never) => {},
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_of_bools_with_ellipsis_at_end_missing_arm() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match (false, true, false) {
|
||||
//^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
(false, ..) => (),
|
||||
}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_of_bools_with_ellipsis_at_beginning_missing_arm() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match (false, true, false) {
|
||||
//^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
(.., false) => (),
|
||||
}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_of_bools_with_ellipsis_in_middle_missing_arm() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match (false, true, false) {
|
||||
//^^^^^^^^^^^^^^^^^^^^ missing match arm
|
||||
(true, .., false) => (),
|
||||
}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_struct() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"struct Foo { a: bool }
|
||||
fn main(f: Foo) {
|
||||
match f {}
|
||||
//^ missing match arm
|
||||
match f { Foo { a: true } => () }
|
||||
//^ missing match arm
|
||||
match &f { Foo { a: true } => () }
|
||||
//^^ missing match arm
|
||||
match f { Foo { a: _ } => () }
|
||||
match f {
|
||||
Foo { a: true } => (),
|
||||
Foo { a: false } => (),
|
||||
}
|
||||
match &f {
|
||||
Foo { a: true } => (),
|
||||
Foo { a: false } => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_struct() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"struct Foo(bool);
|
||||
fn main(f: Foo) {
|
||||
match f {}
|
||||
//^ missing match arm
|
||||
match f { Foo(true) => () }
|
||||
//^ missing match arm
|
||||
match f {
|
||||
Foo(true) => (),
|
||||
Foo(false) => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unit_struct() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"struct Foo;
|
||||
fn main(f: Foo) {
|
||||
match f {}
|
||||
//^ missing match arm
|
||||
match f { Foo => () }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_struct_ellipsis() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"struct Foo { foo: bool, bar: bool }
|
||||
fn main(f: Foo) {
|
||||
match f { Foo { foo: true, .. } => () }
|
||||
//^ missing match arm
|
||||
match f {
|
||||
//^ missing match arm
|
||||
Foo { foo: true, .. } => (),
|
||||
Foo { bar: false, .. } => ()
|
||||
}
|
||||
match f { Foo { .. } => () }
|
||||
match f {
|
||||
Foo { foo: true, .. } => (),
|
||||
Foo { foo: false, .. } => ()
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internal_or() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
enum Either { A(bool), B }
|
||||
match Either::B {
|
||||
//^^^^^^^^^ missing match arm
|
||||
Either::A(true | false) => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_panic_at_unimplemented_subpattern_type() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 1);
|
||||
|
||||
check_diagnostics(
|
||||
r#"
|
||||
struct S { a: char}
|
||||
fn main(v: S) {
|
||||
match v { S{ a } => {} }
|
||||
match v { S{ a: _x } => {} }
|
||||
match v { S{ a: 'a' } => {} }
|
||||
match v { S{..} => {} }
|
||||
match v { _ => {} }
|
||||
match v { }
|
||||
//^ missing match arm
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binding() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match true {
|
||||
_x @ true => {}
|
||||
false => {}
|
||||
}
|
||||
match true { _x @ true => {} }
|
||||
//^^^^ missing match arm
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binding_ref_has_correct_type() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 1);
|
||||
|
||||
// Asserts `PatKind::Binding(ref _x): bool`, not &bool.
|
||||
// If that's not true match checking will panic with "incompatible constructors"
|
||||
// FIXME: make facilities to test this directly like `tests::check_infer(..)`
|
||||
check_diagnostics(
|
||||
r#"
|
||||
enum Foo { A }
|
||||
fn main() {
|
||||
// FIXME: this should not bail out but current behavior is such as the old algorithm.
|
||||
// ExprValidator::validate_match(..) checks types of top level patterns incorrecly.
|
||||
match Foo::A {
|
||||
ref _x => {}
|
||||
Foo::A => {}
|
||||
}
|
||||
match (true,) {
|
||||
(ref _x,) => {}
|
||||
(true,) => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_non_exhaustive() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
//- /lib.rs crate:lib
|
||||
#[non_exhaustive]
|
||||
pub enum E { A, B }
|
||||
fn _local() {
|
||||
match E::A { _ => {} }
|
||||
match E::A {
|
||||
E::A => {}
|
||||
E::B => {}
|
||||
}
|
||||
match E::A {
|
||||
E::A | E::B => {}
|
||||
}
|
||||
}
|
||||
|
||||
//- /main.rs crate:main deps:lib
|
||||
use lib::E;
|
||||
fn main() {
|
||||
match E::A { _ => {} }
|
||||
match E::A {
|
||||
//^^^^ missing match arm
|
||||
E::A => {}
|
||||
E::B => {}
|
||||
}
|
||||
match E::A {
|
||||
//^^^^ missing match arm
|
||||
E::A | E::B => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_guard() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
fn main() {
|
||||
match true {
|
||||
true if false => {}
|
||||
true => {}
|
||||
false => {}
|
||||
}
|
||||
match true {
|
||||
//^^^^ missing match arm
|
||||
true if false => {}
|
||||
false => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_type_is_of_substitution() {
|
||||
cov_mark::check!(match_check_wildcard_expanded_to_substitutions);
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
struct Foo<T>(T);
|
||||
struct Bar;
|
||||
fn main() {
|
||||
match Foo(Bar) {
|
||||
_ | Foo(Bar) => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_struct_no_such_field() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 1);
|
||||
|
||||
check_diagnostics(
|
||||
r#"
|
||||
struct Foo { }
|
||||
fn main(f: Foo) {
|
||||
match f { Foo { bar } => () }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_ergonomics_issue_9095() {
|
||||
check_diagnostics_no_bails(
|
||||
r#"
|
||||
enum Foo<T> { A(T) }
|
||||
fn main() {
|
||||
match &Foo::A(true) {
|
||||
_ => {}
|
||||
Foo::A(_) => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
mod false_negatives {
|
||||
//! The implementation of match checking here is a work in progress. As we roll this out, we
|
||||
//! prefer false negatives to false positives (ideally there would be no false positives). This
|
||||
//! test module should document known false negatives. Eventually we will have a complete
|
||||
//! implementation of match checking and this module will be empty.
|
||||
//!
|
||||
//! The reasons for documenting known false negatives:
|
||||
//!
|
||||
//! 1. It acts as a backlog of work that can be done to improve the behavior of the system.
|
||||
//! 2. It ensures the code doesn't panic when handling these cases.
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn integers() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 1);
|
||||
|
||||
// We don't currently check integer exhaustiveness.
|
||||
check_diagnostics(
|
||||
r#"
|
||||
fn main() {
|
||||
match 5 {
|
||||
10 => (),
|
||||
11..20 => (),
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reference_patterns_at_top_level() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 1);
|
||||
|
||||
check_diagnostics(
|
||||
r#"
|
||||
fn main() {
|
||||
match &false {
|
||||
&true => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reference_patterns_in_fields() {
|
||||
cov_mark::check_count!(validate_match_bailed_out, 2);
|
||||
|
||||
check_diagnostics(
|
||||
r#"
|
||||
fn main() {
|
||||
match (&false,) {
|
||||
(true,) => {}
|
||||
}
|
||||
match (&false,) {
|
||||
(&true,) => {}
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user