Skip to content

Commit 8c5c0ef

Browse files
bors[bot]matklad
andauthored
Merge #9256
9256: internal: kill diagnostic sink r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 7653066 + ff52167 commit 8c5c0ef

File tree

6 files changed

+965
-1157
lines changed

6 files changed

+965
-1157
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@
33
//!
44
//! This probably isn't the best way to do this -- ideally, diagnistics should
55
//! be expressed in terms of hir types themselves.
6-
use std::any::Any;
7-
86
use cfg::{CfgExpr, CfgOptions};
97
use either::Either;
108
use hir_def::path::ModPath;
119
use hir_expand::{name::Name, HirFileId, InFile};
1210
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
1311

14-
pub use crate::diagnostics_sink::{
15-
Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder,
16-
};
17-
1812
macro_rules! diagnostics {
1913
($($diag:ident,)*) => {
2014
pub enum AnyDiagnostic {$(
@@ -38,6 +32,7 @@ diagnostics![
3832
MacroError,
3933
MismatchedArgCount,
4034
MissingFields,
35+
MissingMatchArms,
4136
MissingOkOrSomeInTailExpr,
4237
MissingUnsafe,
4338
NoSuchField,
@@ -149,50 +144,11 @@ pub struct MissingOkOrSomeInTailExpr {
149144
pub required: String,
150145
}
151146

152-
// Diagnostic: missing-match-arm
153-
//
154-
// This diagnostic is triggered if `match` block is missing one or more match arms.
155147
#[derive(Debug)]
156148
pub struct MissingMatchArms {
157149
pub file: HirFileId,
158150
pub match_expr: AstPtr<ast::Expr>,
159151
pub arms: AstPtr<ast::MatchArmList>,
160152
}
161153

162-
impl Diagnostic for MissingMatchArms {
163-
fn code(&self) -> DiagnosticCode {
164-
DiagnosticCode("missing-match-arm")
165-
}
166-
fn message(&self) -> String {
167-
String::from("Missing match arm")
168-
}
169-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
170-
InFile { file_id: self.file, value: self.match_expr.clone().into() }
171-
}
172-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
173-
self
174-
}
175-
}
176-
177-
#[derive(Debug)]
178-
pub struct InternalBailedOut {
179-
pub file: HirFileId,
180-
pub pat_syntax_ptr: SyntaxNodePtr,
181-
}
182-
183-
impl Diagnostic for InternalBailedOut {
184-
fn code(&self) -> DiagnosticCode {
185-
DiagnosticCode("internal:match-check-bailed-out")
186-
}
187-
fn message(&self) -> String {
188-
format!("Internal: match check bailed out")
189-
}
190-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
191-
InFile { file_id: self.file, value: self.pat_syntax_ptr.clone() }
192-
}
193-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
194-
self
195-
}
196-
}
197-
198154
pub use hir_ty::diagnostics::IncorrectCase;

crates/hir/src/diagnostics_sink.rs

Lines changed: 0 additions & 109 deletions
This file was deleted.

crates/hir/src/lib.rs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ mod attrs;
2727
mod has_source;
2828

2929
pub mod diagnostics;
30-
pub mod diagnostics_sink;
3130
pub mod db;
3231

3332
mod display;
@@ -78,16 +77,13 @@ use syntax::{
7877
};
7978
use tt::{Ident, Leaf, Literal, TokenTree};
8079

81-
use crate::{
82-
db::{DefDatabase, HirDatabase},
83-
diagnostics_sink::DiagnosticSink,
84-
};
80+
use crate::db::{DefDatabase, HirDatabase};
8581

8682
pub use crate::{
8783
attrs::{HasAttrs, Namespace},
8884
diagnostics::{
89-
AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase, InternalBailedOut,
90-
MacroError, MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr,
85+
AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase, MacroError,
86+
MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr,
9187
MissingUnsafe, NoSuchField, RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap,
9288
UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall,
9389
UnresolvedModule, UnresolvedProcMacro,
@@ -457,16 +453,10 @@ impl Module {
457453
self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of((*def).into())
458454
}
459455

460-
pub fn diagnostics(
461-
self,
462-
db: &dyn HirDatabase,
463-
sink: &mut DiagnosticSink,
464-
internal_diagnostics: bool,
465-
) -> Vec<AnyDiagnostic> {
456+
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
466457
let _p = profile::span("Module::diagnostics").detail(|| {
467458
format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
468459
});
469-
let mut acc: Vec<AnyDiagnostic> = Vec::new();
470460
let def_map = self.id.def_map(db.upcast());
471461
for diag in def_map.diagnostics() {
472462
if diag.in_module != self.id.local_id {
@@ -619,11 +609,11 @@ impl Module {
619609
}
620610
for decl in self.declarations(db) {
621611
match decl {
622-
ModuleDef::Function(f) => acc.extend(f.diagnostics(db, sink, internal_diagnostics)),
612+
ModuleDef::Function(f) => f.diagnostics(db, acc),
623613
ModuleDef::Module(m) => {
624614
// Only add diagnostics from inline modules
625615
if def_map[m.id.local_id].origin.is_inline() {
626-
acc.extend(m.diagnostics(db, sink, internal_diagnostics))
616+
m.diagnostics(db, acc)
627617
}
628618
}
629619
_ => acc.extend(decl.diagnostics(db)),
@@ -633,11 +623,10 @@ impl Module {
633623
for impl_def in self.impl_defs(db) {
634624
for item in impl_def.items(db) {
635625
if let AssocItem::Function(f) = item {
636-
acc.extend(f.diagnostics(db, sink, internal_diagnostics));
626+
f.diagnostics(db, acc);
637627
}
638628
}
639629
}
640-
acc
641630
}
642631

643632
pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
@@ -1036,13 +1025,7 @@ impl Function {
10361025
db.function_data(self.id).is_async()
10371026
}
10381027

1039-
pub fn diagnostics(
1040-
self,
1041-
db: &dyn HirDatabase,
1042-
sink: &mut DiagnosticSink,
1043-
internal_diagnostics: bool,
1044-
) -> Vec<AnyDiagnostic> {
1045-
let mut acc: Vec<AnyDiagnostic> = Vec::new();
1028+
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
10461029
let krate = self.module(db).id.krate();
10471030

10481031
let source_map = db.body_with_source_map(self.id.into()).1;
@@ -1100,9 +1083,7 @@ impl Function {
11001083
}
11011084
}
11021085

1103-
for diagnostic in
1104-
BodyValidationDiagnostic::collect(db, self.id.into(), internal_diagnostics)
1105-
{
1086+
for diagnostic in BodyValidationDiagnostic::collect(db, self.id.into()) {
11061087
match diagnostic {
11071088
BodyValidationDiagnostic::RecordMissingFields {
11081089
record,
@@ -1209,36 +1190,26 @@ impl Function {
12091190
if let (Some(match_expr), Some(arms)) =
12101191
(match_expr.expr(), match_expr.match_arm_list())
12111192
{
1212-
sink.push(MissingMatchArms {
1213-
file: source_ptr.file_id,
1214-
match_expr: AstPtr::new(&match_expr),
1215-
arms: AstPtr::new(&arms),
1216-
})
1193+
acc.push(
1194+
MissingMatchArms {
1195+
file: source_ptr.file_id,
1196+
match_expr: AstPtr::new(&match_expr),
1197+
arms: AstPtr::new(&arms),
1198+
}
1199+
.into(),
1200+
)
12171201
}
12181202
}
12191203
}
12201204
Err(SyntheticSyntax) => (),
12211205
}
12221206
}
1223-
BodyValidationDiagnostic::InternalBailedOut { pat } => {
1224-
match source_map.pat_syntax(pat) {
1225-
Ok(source_ptr) => {
1226-
let pat_syntax_ptr = source_ptr.value.either(Into::into, Into::into);
1227-
sink.push(InternalBailedOut {
1228-
file: source_ptr.file_id,
1229-
pat_syntax_ptr,
1230-
});
1231-
}
1232-
Err(SyntheticSyntax) => (),
1233-
}
1234-
}
12351207
}
12361208
}
12371209

12381210
for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) {
12391211
acc.push(diag.into())
12401212
}
1241-
acc
12421213
}
12431214

12441215
/// Whether this function declaration has a definition.

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,13 @@ pub enum BodyValidationDiagnostic {
5050
MissingMatchArms {
5151
match_expr: ExprId,
5252
},
53-
InternalBailedOut {
54-
pat: PatId,
55-
},
5653
}
5754

5855
impl BodyValidationDiagnostic {
59-
pub fn collect(
60-
db: &dyn HirDatabase,
61-
owner: DefWithBodyId,
62-
internal_diagnostics: bool,
63-
) -> Vec<BodyValidationDiagnostic> {
56+
pub fn collect(db: &dyn HirDatabase, owner: DefWithBodyId) -> Vec<BodyValidationDiagnostic> {
6457
let _p = profile::span("BodyValidationDiagnostic::collect");
6558
let infer = db.infer(owner);
6659
let mut validator = ExprValidator::new(owner, infer.clone());
67-
validator.internal_diagnostics = internal_diagnostics;
6860
validator.validate_body(db);
6961
validator.diagnostics
7062
}
@@ -74,12 +66,11 @@ struct ExprValidator {
7466
owner: DefWithBodyId,
7567
infer: Arc<InferenceResult>,
7668
pub(super) diagnostics: Vec<BodyValidationDiagnostic>,
77-
internal_diagnostics: bool,
7869
}
7970

8071
impl ExprValidator {
8172
fn new(owner: DefWithBodyId, infer: Arc<InferenceResult>) -> ExprValidator {
82-
ExprValidator { owner, infer, diagnostics: Vec::new(), internal_diagnostics: false }
73+
ExprValidator { owner, infer, diagnostics: Vec::new() }
8374
}
8475

8576
fn validate_body(&mut self, db: &dyn HirDatabase) {
@@ -308,9 +299,7 @@ impl ExprValidator {
308299
// fit the match expression, we skip this diagnostic. Skipping the entire
309300
// diagnostic rather than just not including this match arm is preferred
310301
// to avoid the chance of false positives.
311-
if self.internal_diagnostics {
312-
self.diagnostics.push(BodyValidationDiagnostic::InternalBailedOut { pat: arm.pat })
313-
}
302+
cov_mark::hit!(validate_match_bailed_out);
314303
return;
315304
}
316305

0 commit comments

Comments
 (0)