Skip to content

Commit 0436bf1

Browse files
committed
Auto merge of rust-lang#125183 - cuviper:beta-next, r=cuviper
[beta] backports - Do not ICE on foreign malformed `diagnostic::on_unimplemented` rust-lang#124683 - Fix more ICEs in `diagnostic::on_unimplemented` rust-lang#124875 - rustdoc: use stability, instead of features, to decide what to show rust-lang#124864 - Don't do post-method-probe error reporting steps if we're in a suggestion rust-lang#125100 - Make `non-local-def` lint Allow by default rust-lang#124950 r? cuviper
2 parents a269819 + cfae8b2 commit 0436bf1

37 files changed

+725
-171
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
942942
);
943943
}
944944

945-
pub fn get_conversion_methods(
945+
pub fn get_conversion_methods_for_diagnostic(
946946
&self,
947947
span: Span,
948948
expected: Ty<'tcx>,
949949
checked_ty: Ty<'tcx>,
950950
hir_id: hir::HirId,
951951
) -> Vec<AssocItem> {
952-
let methods = self.probe_for_return_type(
952+
let methods = self.probe_for_return_type_for_diagnostic(
953953
span,
954954
probe::Mode::MethodCall,
955955
expected,

compiler/rustc_hir_typeck/src/expr.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24142414

24152415
let guar = if field.name == kw::Empty {
24162416
self.dcx().span_delayed_bug(field.span, "field name with no name")
2417-
} else if self.method_exists(field, base_ty, expr.hir_id, expected.only_has_type(self)) {
2417+
} else if self.method_exists_for_diagnostic(
2418+
field,
2419+
base_ty,
2420+
expr.hir_id,
2421+
expected.only_has_type(self),
2422+
) {
24182423
self.ban_take_value_of_method(expr, base_ty, field)
24192424
} else if !base_ty.is_primitive_ty() {
24202425
self.ban_nonexisting_field(field, base, expr, base_ty)
@@ -2600,7 +2605,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26002605
let mut err = self.private_field_err(field, base_did);
26012606

26022607
// Also check if an accessible method exists, which is often what is meant.
2603-
if self.method_exists(field, expr_t, expr.hir_id, return_ty)
2608+
if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
26042609
&& !self.expr_in_place(expr.hir_id)
26052610
{
26062611
self.suggest_method_call(

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
290290
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
291291
) -> bool {
292292
let expr = expr.peel_blocks();
293-
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);
293+
let methods =
294+
self.get_conversion_methods_for_diagnostic(expr.span, expected, found, expr.hir_id);
294295

295296
if let Some((suggestion, msg, applicability, verbose, annotation)) =
296297
self.suggest_deref_or_ref(expr, found, expected)

compiler/rustc_hir_typeck/src/method/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub enum CandidateSource {
9090
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9191
/// Determines whether the type `self_ty` supports a visible method named `method_name` or not.
9292
#[instrument(level = "debug", skip(self))]
93-
pub fn method_exists(
93+
pub fn method_exists_for_diagnostic(
9494
&self,
9595
method_name: Ident,
9696
self_ty: Ty<'tcx>,
@@ -101,7 +101,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
101101
probe::Mode::MethodCall,
102102
method_name,
103103
return_type,
104-
IsSuggestion(false),
104+
IsSuggestion(true),
105105
self_ty,
106106
call_expr_id,
107107
ProbeScope::TraitsInScope,

compiler/rustc_hir_typeck/src/method/probe.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
8888
>,
8989

9090
scope_expr_id: HirId,
91+
92+
/// Is this probe being done for a diagnostic? This will skip some error reporting
93+
/// machinery, since we don't particularly care about, for example, similarly named
94+
/// candidates if we're *reporting* similarly named candidates.
95+
is_suggestion: IsSuggestion,
9196
}
9297

9398
impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
@@ -218,7 +223,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
218223
/// would use to decide if a method is a plausible fit for
219224
/// ambiguity purposes).
220225
#[instrument(level = "debug", skip(self, candidate_filter))]
221-
pub fn probe_for_return_type(
226+
pub fn probe_for_return_type_for_diagnostic(
222227
&self,
223228
span: Span,
224229
mode: Mode,
@@ -457,6 +462,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
457462
&orig_values,
458463
steps.steps,
459464
scope_expr_id,
465+
is_suggestion,
460466
);
461467

462468
probe_cx.assemble_inherent_candidates();
@@ -551,6 +557,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
551557
orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
552558
steps: &'tcx [CandidateStep<'tcx>],
553559
scope_expr_id: HirId,
560+
is_suggestion: IsSuggestion,
554561
) -> ProbeContext<'a, 'tcx> {
555562
ProbeContext {
556563
fcx,
@@ -568,6 +575,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
568575
static_candidates: RefCell::new(Vec::new()),
569576
unsatisfied_predicates: RefCell::new(Vec::new()),
570577
scope_expr_id,
578+
is_suggestion,
571579
}
572580
}
573581

@@ -942,6 +950,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
942950
return r;
943951
}
944952

953+
// If it's a `lookup_probe_for_diagnostic`, then quit early. No need to
954+
// probe for other candidates.
955+
if self.is_suggestion.0 {
956+
return Err(MethodError::NoMatch(NoMatchData {
957+
static_candidates: vec![],
958+
unsatisfied_predicates: vec![],
959+
out_of_scope_traits: vec![],
960+
similar_candidate: None,
961+
mode: self.mode,
962+
}));
963+
}
964+
945965
debug!("pick: actual search failed, assemble diagnostics");
946966

947967
let static_candidates = std::mem::take(self.static_candidates.get_mut());
@@ -1633,6 +1653,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16331653
self.orig_steps_var_values,
16341654
self.steps,
16351655
self.scope_expr_id,
1656+
IsSuggestion(true),
16361657
);
16371658
pcx.allow_similar_names = true;
16381659
pcx.assemble_inherent_candidates();

compiler/rustc_hir_typeck/src/method/suggest.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28352835
Some(output_ty) => self.resolve_vars_if_possible(output_ty),
28362836
_ => return,
28372837
};
2838-
let method_exists = self.method_exists(item_name, output_ty, call.hir_id, return_type);
2838+
let method_exists =
2839+
self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
28392840
debug!("suggest_await_before_method: is_method_exist={}", method_exists);
28402841
if method_exists {
28412842
err.span_suggestion_verbose(

compiler/rustc_lint/src/non_local_def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ declare_lint! {
4646
/// All nested bodies (functions, enum discriminant, array length, consts) (expect for
4747
/// `const _: Ty = { ... }` in top-level module, which is still undecided) are checked.
4848
pub NON_LOCAL_DEFINITIONS,
49-
Warn,
49+
Allow,
5050
"checks for non-local definitions",
5151
report_in_external_macro
5252
}

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+77-57
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,14 @@ impl IgnoredDiagnosticOption {
349349
option_name: &'static str,
350350
) {
351351
if let (Some(new_item), Some(old_item)) = (new, old) {
352-
tcx.emit_node_span_lint(
353-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
354-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
355-
new_item,
356-
IgnoredDiagnosticOption { span: new_item, prev_span: old_item, option_name },
357-
);
352+
if let Some(item_def_id) = item_def_id.as_local() {
353+
tcx.emit_node_span_lint(
354+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
355+
tcx.local_def_id_to_hir_id(item_def_id),
356+
new_item,
357+
IgnoredDiagnosticOption { span: new_item, prev_span: old_item, option_name },
358+
);
359+
}
358360
}
359361
}
360362
}
@@ -498,12 +500,14 @@ impl<'tcx> OnUnimplementedDirective {
498500
}
499501

500502
if is_diagnostic_namespace_variant {
501-
tcx.emit_node_span_lint(
502-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
503-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
504-
vec![item.span()],
505-
MalformedOnUnimplementedAttrLint::new(item.span()),
506-
);
503+
if let Some(def_id) = item_def_id.as_local() {
504+
tcx.emit_node_span_lint(
505+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
506+
tcx.local_def_id_to_hir_id(def_id),
507+
vec![item.span()],
508+
MalformedOnUnimplementedAttrLint::new(item.span()),
509+
);
510+
}
507511
} else {
508512
// nothing found
509513
tcx.dcx().emit_err(NoValueInOnUnimplemented { span: item.span() });
@@ -636,30 +640,38 @@ impl<'tcx> OnUnimplementedDirective {
636640
AttrArgs::Eq(span, AttrArgsEq::Hir(expr)) => span.to(expr.span),
637641
};
638642

639-
tcx.emit_node_span_lint(
640-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
641-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
642-
report_span,
643-
MalformedOnUnimplementedAttrLint::new(report_span),
644-
);
643+
if let Some(item_def_id) = item_def_id.as_local() {
644+
tcx.emit_node_span_lint(
645+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
646+
tcx.local_def_id_to_hir_id(item_def_id),
647+
report_span,
648+
MalformedOnUnimplementedAttrLint::new(report_span),
649+
);
650+
}
645651
Ok(None)
646652
}
647653
} else if is_diagnostic_namespace_variant {
648654
match &attr.kind {
649655
AttrKind::Normal(p) if !matches!(p.item.args, AttrArgs::Empty) => {
650-
tcx.emit_node_span_lint(
651-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
652-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
653-
attr.span,
654-
MalformedOnUnimplementedAttrLint::new(attr.span),
655-
);
656+
if let Some(item_def_id) = item_def_id.as_local() {
657+
tcx.emit_node_span_lint(
658+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
659+
tcx.local_def_id_to_hir_id(item_def_id),
660+
attr.span,
661+
MalformedOnUnimplementedAttrLint::new(attr.span),
662+
);
663+
}
664+
}
665+
_ => {
666+
if let Some(item_def_id) = item_def_id.as_local() {
667+
tcx.emit_node_span_lint(
668+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
669+
tcx.local_def_id_to_hir_id(item_def_id),
670+
attr.span,
671+
MissingOptionsForOnUnimplementedAttr,
672+
)
673+
}
656674
}
657-
_ => tcx.emit_node_span_lint(
658-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
659-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
660-
attr.span,
661-
MissingOptionsForOnUnimplementedAttr,
662-
),
663675
};
664676

665677
Ok(None)
@@ -788,12 +800,14 @@ impl<'tcx> OnUnimplementedFormatString {
788800
|| format_spec.precision_span.is_some()
789801
|| format_spec.fill_span.is_some())
790802
{
791-
tcx.emit_node_span_lint(
792-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
793-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
794-
self.span,
795-
InvalidFormatSpecifier,
796-
);
803+
if let Some(item_def_id) = item_def_id.as_local() {
804+
tcx.emit_node_span_lint(
805+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
806+
tcx.local_def_id_to_hir_id(item_def_id),
807+
self.span,
808+
InvalidFormatSpecifier,
809+
);
810+
}
797811
}
798812
match a.position {
799813
Position::ArgumentNamed(s) => {
@@ -809,15 +823,17 @@ impl<'tcx> OnUnimplementedFormatString {
809823
s if generics.params.iter().any(|param| param.name == s) => (),
810824
s => {
811825
if self.is_diagnostic_namespace_variant {
812-
tcx.emit_node_span_lint(
813-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
814-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
815-
self.span,
816-
UnknownFormatParameterForOnUnimplementedAttr {
817-
argument_name: s,
818-
trait_name,
819-
},
820-
);
826+
if let Some(item_def_id) = item_def_id.as_local() {
827+
tcx.emit_node_span_lint(
828+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
829+
tcx.local_def_id_to_hir_id(item_def_id),
830+
self.span,
831+
UnknownFormatParameterForOnUnimplementedAttr {
832+
argument_name: s,
833+
trait_name,
834+
},
835+
);
836+
}
821837
} else {
822838
result = Err(struct_span_code_err!(
823839
tcx.dcx(),
@@ -839,12 +855,14 @@ impl<'tcx> OnUnimplementedFormatString {
839855
// `{:1}` and `{}` are not to be used
840856
Position::ArgumentIs(..) | Position::ArgumentImplicitlyIs(_) => {
841857
if self.is_diagnostic_namespace_variant {
842-
tcx.emit_node_span_lint(
843-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
844-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
845-
self.span,
846-
DisallowedPositionalArgument,
847-
);
858+
if let Some(item_def_id) = item_def_id.as_local() {
859+
tcx.emit_node_span_lint(
860+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
861+
tcx.local_def_id_to_hir_id(item_def_id),
862+
self.span,
863+
DisallowedPositionalArgument,
864+
);
865+
}
848866
} else {
849867
let reported = struct_span_code_err!(
850868
tcx.dcx(),
@@ -867,12 +885,14 @@ impl<'tcx> OnUnimplementedFormatString {
867885
// so that users are aware that something is not correct
868886
for e in parser.errors {
869887
if self.is_diagnostic_namespace_variant {
870-
tcx.emit_node_span_lint(
871-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
872-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
873-
self.span,
874-
WrappedParserError { description: e.description, label: e.label },
875-
);
888+
if let Some(item_def_id) = item_def_id.as_local() {
889+
tcx.emit_node_span_lint(
890+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
891+
tcx.local_def_id_to_hir_id(item_def_id),
892+
self.span,
893+
WrappedParserError { description: e.description, label: e.label },
894+
);
895+
}
876896
} else {
877897
let reported =
878898
struct_span_code_err!(tcx.dcx(), self.span, E0231, "{}", e.description,).emit();

0 commit comments

Comments
 (0)