Don't show unresolved-field diagnostic for missing names

This commit is contained in:
Ryo Yoshida 2023-07-06 20:35:54 +09:00
parent e95644e279
commit 827a0530bc
No known key found for this signature in database
GPG Key ID: E25698A930586171
3 changed files with 27 additions and 1 deletions

View File

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

View File

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

View File

@ -68,7 +68,10 @@ fn method_fix(
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests::check_diagnostics; use crate::{
tests::{check_diagnostics, check_diagnostics_with_config},
DiagnosticsConfig,
};
#[test] #[test]
fn smoke_test() { fn smoke_test() {
@ -146,4 +149,11 @@ fn foo() {
"#, "#,
); );
} }
#[test]
fn no_diagnostic_for_missing_name() {
let mut config = DiagnosticsConfig::test_sample();
config.disabled.insert("syntax-error".to_owned());
check_diagnostics_with_config(config, "fn foo() { (). }");
}
} }