Skip to content

Commit d56b304

Browse files
committed
Migrate SuggestAccessingField
1 parent b36abea commit d56b304

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

compiler/rustc_infer/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,5 @@ infer_fn_consider_casting = consider casting the fn item to a fn pointer: `{$cas
360360
361361
infer_sarwa_option = you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`
362362
infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` using `.as_ref()`
363+
364+
infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}`

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,3 +1289,31 @@ pub enum SuggestAsRefWhereAppropriate<'a> {
12891289
snippet: &'a str,
12901290
},
12911291
}
1292+
1293+
#[derive(Subdiagnostic)]
1294+
pub enum SuggestAccessingField<'a> {
1295+
#[suggestion(
1296+
infer_suggest_accessing_field,
1297+
code = "{snippet}.{name}",
1298+
applicability = "maybe-incorrect"
1299+
)]
1300+
Safe {
1301+
#[primary_span]
1302+
span: Span,
1303+
snippet: String,
1304+
name: Symbol,
1305+
ty: Ty<'a>,
1306+
},
1307+
#[suggestion(
1308+
infer_suggest_accessing_field,
1309+
code = "unsafe {{ {snippet}.{name} }}",
1310+
applicability = "maybe-incorrect"
1311+
)]
1312+
Unsafe {
1313+
#[primary_span]
1314+
span: Span,
1315+
snippet: String,
1316+
name: Symbol,
1317+
ty: Ty<'a>,
1318+
},
1319+
}

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_target::abi::FieldIdx;
1414

1515
use crate::errors::{
1616
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
17-
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAsRefWhereAppropriate,
18-
SuggestRemoveSemiOrReturnBinding,
17+
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
18+
SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding,
1919
};
2020

2121
use super::TypeErrCtxt;
@@ -264,15 +264,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
264264
}
265265
}
266266

267-
pub fn suggest_await_on_future(&self, diag: &mut Diagnostic, sp: Span) {
268-
diag.span_suggestion_verbose(
269-
sp.shrink_to_hi(),
270-
"consider `await`ing on the `Future`",
271-
".await",
272-
Applicability::MaybeIncorrect,
273-
);
274-
}
275-
276267
pub(super) fn suggest_accessing_field_where_appropriate(
277268
&self,
278269
cause: &ObligationCause<'tcx>,
@@ -299,21 +290,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
299290
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {
300291
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
301292
let suggestion = if expected_def.is_struct() {
302-
format!("{}.{}", snippet, name)
293+
SuggestAccessingField::Safe { span, snippet, name, ty }
303294
} else if expected_def.is_union() {
304-
format!("unsafe {{ {}.{} }}", snippet, name)
295+
SuggestAccessingField::Unsafe { span, snippet, name, ty }
305296
} else {
306297
return;
307298
};
308-
diag.span_suggestion(
309-
span,
310-
&format!(
311-
"you might have meant to use field `{}` whose type is `{}`",
312-
name, ty
313-
),
314-
suggestion,
315-
Applicability::MaybeIncorrect,
316-
);
299+
diag.subdiagnostic(suggestion);
317300
}
318301
}
319302
}

0 commit comments

Comments
 (0)