Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
745c600
Talk about `gen fn` in diagnostics about `gen fn`
oli-obk Oct 30, 2023
bc926f7
Add a custom panic message for resuming `gen` blocks after they panicked
oli-obk Oct 30, 2023
972ee01
Add regression test
oli-obk Oct 30, 2023
8d03e13
Don't treat closures/coroutines as part of the public API
oli-obk Oct 30, 2023
251021c
Merge two equal match arms
oli-obk Oct 30, 2023
43ff2a7
Some manual rustfmt as rustfmt is broken on this file
oli-obk Oct 30, 2023
d5e836c
Correctly handle nested or-patterns in column-wise analyses
Nadrieril Oct 30, 2023
ff3a818
Poison check_well_formed if method receivers are invalid to prevent t…
oli-obk Oct 30, 2023
224ddf8
Only run panic tests on targets that can unwind
oli-obk Oct 30, 2023
162443b
Detect when trait is implemented for type and suggest importing it
estebank Oct 17, 2023
455cf5a
Improve some diagnostics around `?Trait` bounds
oli-obk Oct 30, 2023
c91f60e
Don't super-fold types when we hit the recursion limit
compiler-errors Oct 30, 2023
c299595
Rollup merge of #116862 - estebank:issue-57457, r=oli-obk
matthiaskrgr Oct 30, 2023
86259e7
Rollup merge of #117389 - oli-obk:gen_fn, r=compiler-errors
matthiaskrgr Oct 30, 2023
e648f47
Rollup merge of #117396 - oli-obk:privacy_visitor_types, r=compiler-e…
matthiaskrgr Oct 30, 2023
342483c
Rollup merge of #117398 - Nadrieril:fix-117378, r=compiler-errors
matthiaskrgr Oct 30, 2023
24c6b6c
Rollup merge of #117403 - oli-obk:the_gift_that_keeps_on_giving_11684…
matthiaskrgr Oct 30, 2023
3e95c6a
Rollup merge of #117411 - oli-obk:query_merge_immobile_game, r=compil…
matthiaskrgr Oct 30, 2023
c5aec96
Rollup merge of #117414 - compiler-errors:tait-forevert, r=oli-obk
matthiaskrgr Oct 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use rustc_span::def_id::DefIdSet;
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Symbol;
use rustc_span::{edit_distance, source_map, ExpnKind, FileName, MacroKind, Span};
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplementedNote;
use rustc_trait_selection::traits::error_reporting::on_unimplemented::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
Expand Down Expand Up @@ -192,7 +193,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.span_if_local(def_id)
.unwrap_or_else(|| self.tcx.def_span(def_id));
err.span_label(sp, format!("private {kind} defined here"));
self.suggest_valid_traits(&mut err, out_of_scope_traits);
self.suggest_valid_traits(&mut err, out_of_scope_traits, true);
err.emit();
}

Expand Down Expand Up @@ -2464,6 +2465,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
err: &mut Diagnostic,
valid_out_of_scope_traits: Vec<DefId>,
explain: bool,
) -> bool {
if !valid_out_of_scope_traits.is_empty() {
let mut candidates = valid_out_of_scope_traits;
Expand All @@ -2476,7 +2478,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
.copied();

err.help("items from traits can only be used if the trait is in scope");
if explain {
err.help("items from traits can only be used if the trait is in scope");
}
let msg = format!(
"the following {traits_are} implemented but not in scope; \
perhaps add a `use` for {one_of_them}:",
Expand Down Expand Up @@ -2693,7 +2697,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}
if self.suggest_valid_traits(err, valid_out_of_scope_traits) {
if self.suggest_valid_traits(err, valid_out_of_scope_traits, true) {
return;
}

Expand Down Expand Up @@ -2970,29 +2974,49 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(candidates, Vec::new())
};

let impls_trait = |def_id: DefId| {
let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
if param.index == 0 {
rcvr_ty.into()
} else {
self.infcx.var_for_def(span, param)
}
});
self.infcx
.type_implements_trait(def_id, args, self.param_env)
.must_apply_modulo_regions()
&& param_type.is_none()
};
match &potential_candidates[..] {
[] => {}
[trait_info] if trait_info.def_id.is_local() => {
err.subdiagnostic(CandidateTraitNote {
span: self.tcx.def_span(trait_info.def_id),
trait_name: self.tcx.def_path_str(trait_info.def_id),
item_name,
action_or_ty: if trait_missing_method {
"NONE".to_string()
} else {
param_type.map_or_else(
|| "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
ToString::to_string,
)
},
});
if impls_trait(trait_info.def_id) {
self.suggest_valid_traits(err, vec![trait_info.def_id], false);
} else {
err.subdiagnostic(CandidateTraitNote {
span: self.tcx.def_span(trait_info.def_id),
trait_name: self.tcx.def_path_str(trait_info.def_id),
item_name,
action_or_ty: if trait_missing_method {
"NONE".to_string()
} else {
param_type.map_or_else(
|| "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
ToString::to_string,
)
},
});
}
}
trait_infos => {
let mut msg = message(param_type.map_or_else(
|| "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
|param| format!("restrict type parameter `{param}` with"),
));
for (i, trait_info) in trait_infos.iter().enumerate() {
if impls_trait(trait_info.def_id) {
self.suggest_valid_traits(err, vec![trait_info.def_id], false);
}
msg.push_str(&format!(
"\ncandidate #{}: `{}`",
i + 1,
Expand Down
21 changes: 9 additions & 12 deletions tests/ui/traits/item-privacy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ LL | S.a();
| ^ method not found in `S`
|
= help: items from traits can only be used if the trait is implemented and in scope
note: `method::A` defines an item `a`, perhaps you need to implement it
--> $DIR/item-privacy.rs:6:5
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use method::A;
|
LL | trait A {
| ^^^^^^^

error[E0599]: no method named `b` found for struct `S` in the current scope
--> $DIR/item-privacy.rs:68:7
Expand Down Expand Up @@ -51,11 +50,10 @@ LL | S::a(&S);
| ^ function or associated item not found in `S`
|
= help: items from traits can only be used if the trait is implemented and in scope
note: `method::A` defines an item `a`, perhaps you need to implement it
--> $DIR/item-privacy.rs:6:5
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use method::A;
|
LL | trait A {
| ^^^^^^^

error[E0599]: no function or associated item named `b` found for struct `S` in the current scope
--> $DIR/item-privacy.rs:80:8
Expand Down Expand Up @@ -91,11 +89,10 @@ LL | S::A;
| ^ associated item not found in `S`
|
= help: items from traits can only be used if the trait is implemented and in scope
note: `assoc_const::A` defines an item `A`, perhaps you need to implement it
--> $DIR/item-privacy.rs:24:5
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use assoc_const::A;
|
LL | trait A {
| ^^^^^^^

error[E0599]: no associated item named `B` found for struct `S` in the current scope
--> $DIR/item-privacy.rs:98:8
Expand Down