Skip to content

Commit db17f79

Browse files
committed
Auto merge of rust-lang#15223 - lowr:patch/no-unresolved-field-for-missing, r=HKalbasi
Don't show `unresolved-field` diagnostic for missing names I don't think reporting ``"no field `[missing name]` on type `SomeType`"`` makes much sense because it's a syntax error rather than a semantic error. We already report a syntax error for it and I find it sufficient.
2 parents 537f9b3 + 827a053 commit db17f79

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

crates/hir-expand/src/name.rs

+9
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ impl Name {
9696
Name::new_inline("[missing name]")
9797
}
9898

99+
/// Returns true if this is a fake name for things missing in the source code. See
100+
/// [`missing()`][Self::missing] for details.
101+
///
102+
/// Use this method instead of comparing with `Self::missing()` as missing names
103+
/// (ideally should) have a `gensym` semantics.
104+
pub fn is_missing(&self) -> bool {
105+
self == &Name::missing()
106+
}
107+
99108
/// Generates a new name which is only equal to itself, by incrementing a counter. Due
100109
/// its implementation, it should not be used in things that salsa considers, like
101110
/// type names or field names, and it should be only used in names of local variables

crates/hir-ty/src/infer/expr.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,13 @@ impl InferenceContext<'_> {
14491449

14501450
fn infer_field_access(&mut self, tgt_expr: ExprId, receiver: ExprId, name: &Name) -> Ty {
14511451
let receiver_ty = self.infer_expr_inner(receiver, &Expectation::none());
1452+
1453+
if name.is_missing() {
1454+
// Bail out early, don't even try to look up field. Also, we don't issue an unresolved
1455+
// field diagnostic because this is a syntax error rather than a semantic error.
1456+
return self.err_ty();
1457+
}
1458+
14521459
match self.lookup_field(&receiver_ty, name) {
14531460
Some((ty, field_id, adjustments, is_public)) => {
14541461
self.write_expr_adj(receiver, adjustments);

crates/ide-diagnostics/src/handlers/unresolved_field.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ fn method_fix(
6868
}
6969
#[cfg(test)]
7070
mod tests {
71-
use crate::tests::check_diagnostics;
71+
use crate::{
72+
tests::{check_diagnostics, check_diagnostics_with_config},
73+
DiagnosticsConfig,
74+
};
7275

7376
#[test]
7477
fn smoke_test() {
@@ -146,4 +149,11 @@ fn foo() {
146149
"#,
147150
);
148151
}
152+
153+
#[test]
154+
fn no_diagnostic_for_missing_name() {
155+
let mut config = DiagnosticsConfig::test_sample();
156+
config.disabled.insert("syntax-error".to_owned());
157+
check_diagnostics_with_config(config, "fn foo() { (). }");
158+
}
149159
}

0 commit comments

Comments
 (0)