Skip to content

Commit 045fc18

Browse files
authored
Rollup merge of #102718 - compiler-errors:opaque-bound-lint-ice, r=fee1-dead
Fix `opaque_hidden_inferred_bound` lint ICE Fixes #102705
2 parents e77e020 + fe05336 commit 045fc18

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,5 @@ lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not
436436
437437
lint_opaque_hidden_inferred_bound = opaque type `{$ty}` does not satisfy its associated type bounds
438438
.specifically = this associated type bound is unsatisfied for `{$proj_ty}`
439-
.suggestion = add this bound
439+
440+
lint_opaque_hidden_inferred_bound_sugg = add this bound

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustc_hir as hir;
22
use rustc_infer::infer::TyCtxtInferExt;
3-
use rustc_macros::LintDiagnostic;
4-
use rustc_middle::ty::{self, fold::BottomUpFolder, Ty, TypeFoldable};
3+
use rustc_macros::{LintDiagnostic, Subdiagnostic};
4+
use rustc_middle::ty::{
5+
self, fold::BottomUpFolder, print::TraitPredPrintModifiersAndPath, Ty, TypeFoldable,
6+
};
57
use rustc_span::Span;
68
use rustc_trait_selection::traits;
79
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
@@ -117,13 +119,13 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
117119
)) {
118120
// If it's a trait bound and an opaque that doesn't satisfy it,
119121
// then we can emit a suggestion to add the bound.
120-
let (suggestion, suggest_span) =
122+
let add_bound =
121123
match (proj_term.kind(), assoc_pred.kind().skip_binder()) {
122-
(ty::Opaque(def_id, _), ty::PredicateKind::Trait(trait_pred)) => (
123-
format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
124-
Some(cx.tcx.def_span(def_id).shrink_to_hi()),
125-
),
126-
_ => (String::new(), None),
124+
(ty::Opaque(def_id, _), ty::PredicateKind::Trait(trait_pred)) => Some(AddBound {
125+
suggest_span: cx.tcx.def_span(*def_id).shrink_to_hi(),
126+
trait_ref: trait_pred.print_modifiers_and_trait_path(),
127+
}),
128+
_ => None,
127129
};
128130
cx.emit_spanned_lint(
129131
OPAQUE_HIDDEN_INFERRED_BOUND,
@@ -132,8 +134,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
132134
ty: cx.tcx.mk_opaque(def_id, ty::InternalSubsts::identity_for_item(cx.tcx, def_id)),
133135
proj_ty: proj_term,
134136
assoc_pred_span,
135-
suggestion,
136-
suggest_span,
137+
add_bound,
137138
},
138139
);
139140
}
@@ -150,7 +151,19 @@ struct OpaqueHiddenInferredBoundLint<'tcx> {
150151
proj_ty: Ty<'tcx>,
151152
#[label(lint::specifically)]
152153
assoc_pred_span: Span,
153-
#[suggestion_verbose(applicability = "machine-applicable", code = "{suggestion}")]
154-
suggest_span: Option<Span>,
155-
suggestion: String,
154+
#[subdiagnostic]
155+
add_bound: Option<AddBound<'tcx>>,
156+
}
157+
158+
#[derive(Subdiagnostic)]
159+
#[suggestion_verbose(
160+
lint::opaque_hidden_inferred_bound_sugg,
161+
applicability = "machine-applicable",
162+
code = " + {trait_ref}"
163+
)]
164+
struct AddBound<'tcx> {
165+
#[primary_span]
166+
suggest_span: Span,
167+
#[skip_arg]
168+
trait_ref: TraitPredPrintModifiersAndPath<'tcx>,
156169
}

src/test/ui/lint/issue-102705.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// check-pass
2+
3+
#![allow(opaque_hidden_inferred_bound)]
4+
#![allow(dead_code)]
5+
6+
trait Duh {}
7+
8+
impl Duh for i32 {}
9+
10+
trait Trait {
11+
type Assoc: Duh;
12+
}
13+
14+
impl<R: Duh, F: FnMut() -> R> Trait for F {
15+
type Assoc = R;
16+
}
17+
18+
fn foo() -> impl Trait<Assoc = impl Send> {
19+
|| 42
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)