Skip to content

Commit a5c8169

Browse files
authored
Rollup merge of #91873 - estebank:mention-impls-for-unsatisfied-trait, r=davidtwco
Mention implementers of unsatisfied trait When encountering an unsatisfied trait bound, if there are no other suggestions, mention all the types that *do* implement that trait: ``` error[E0277]: the trait bound `f32: Foo` is not satisfied --> $DIR/impl_wf.rs:22:6 | LL | impl Baz<f32> for f32 { } | ^^^^^^^^ the trait `Foo` is not implemented for `f32` | = help: the trait `Foo` is implemented for `i32` note: required by a bound in `Baz` --> $DIR/impl_wf.rs:18:31 | LL | trait Baz<U: ?Sized> where U: Foo { } | ^^^ required by this bound in `Baz` ``` ``` error[E0277]: the trait bound `u32: Foo` is not satisfied --> $DIR/associated-types-path-2.rs:29:5 | LL | f1(2u32, 4u32); | ^^ the trait `Foo` is not implemented for `u32` | = help: the trait `Foo` is implemented for `i32` note: required by a bound in `f1` --> $DIR/associated-types-path-2.rs:13:14 | LL | pub fn f1<T: Foo>(a: T, x: T::A) {} | ^^^ required by this bound in `f1` ``` Suggest dereferencing in more cases. Fix #87437, fix #90970.
2 parents 60e50fc + e1ef833 commit a5c8169

File tree

136 files changed

+1241
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+1241
-291
lines changed

compiler/rustc_errors/src/diagnostic.rs

+6
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ impl Diagnostic {
378378
self
379379
}
380380

381+
/// Add a help message attached to this diagnostic with a customizable highlighted message.
382+
pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
383+
self.sub_with_highlights(Level::Help, msg, MultiSpan::new(), None);
384+
self
385+
}
386+
381387
/// Prints the span with some help above it.
382388
/// This is like [`Diagnostic::help()`], but it gets its own span.
383389
pub fn span_help<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self {

compiler/rustc_middle/src/ty/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_hir::Node;
4141
use rustc_macros::HashStable;
4242
use rustc_query_system::ich::StableHashingContext;
4343
use rustc_session::cstore::CrateStoreDyn;
44-
use rustc_span::symbol::{kw, Ident, Symbol};
44+
use rustc_span::symbol::{kw, sym, Ident, Symbol};
4545
use rustc_span::Span;
4646
use rustc_target::abi::Align;
4747

@@ -2206,7 +2206,7 @@ impl<'tcx> TyCtxt<'tcx> {
22062206
self.impl_trait_ref(def_id).map(|tr| tr.def_id)
22072207
}
22082208

2209-
/// If the given defid describes a method belonging to an impl, returns the
2209+
/// If the given `DefId` describes a method belonging to an impl, returns the
22102210
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
22112211
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
22122212
self.opt_associated_item(def_id).and_then(|trait_item| match trait_item.container {
@@ -2215,6 +2215,11 @@ impl<'tcx> TyCtxt<'tcx> {
22152215
})
22162216
}
22172217

2218+
/// If the given `DefId` belongs to a trait that was automatically derived, returns `true`.
2219+
pub fn is_builtin_derive(self, def_id: DefId) -> bool {
2220+
self.has_attr(def_id, sym::automatically_derived)
2221+
}
2222+
22182223
/// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`
22192224
/// with the name of the crate containing the impl.
22202225
pub fn span_of_impl(self, impl_did: DefId) -> Result<Span, Symbol> {

compiler/rustc_mir_transform/src/check_packed_ref.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use rustc_hir::def_id::{DefId, LocalDefId};
1+
use rustc_hir::def_id::LocalDefId;
22
use rustc_middle::mir::visit::{PlaceContext, Visitor};
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::query::Providers;
55
use rustc_middle::ty::{self, TyCtxt};
66
use rustc_session::lint::builtin::UNALIGNED_REFERENCES;
7-
use rustc_span::symbol::sym;
87

98
use crate::util;
109
use crate::MirLint;
@@ -50,22 +49,6 @@ fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
5049
});
5150
}
5251

53-
fn builtin_derive_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
54-
debug!("builtin_derive_def_id({:?})", def_id);
55-
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
56-
if tcx.has_attr(impl_def_id, sym::automatically_derived) {
57-
debug!("builtin_derive_def_id({:?}) - is {:?}", def_id, impl_def_id);
58-
Some(impl_def_id)
59-
} else {
60-
debug!("builtin_derive_def_id({:?}) - not automatically derived", def_id);
61-
None
62-
}
63-
} else {
64-
debug!("builtin_derive_def_id({:?}) - not a method", def_id);
65-
None
66-
}
67-
}
68-
6952
impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
7053
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
7154
// Make sure we know where in the MIR we are.
@@ -83,7 +66,11 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
8366
if context.is_borrow() {
8467
if util::is_disaligned(self.tcx, self.body, self.param_env, *place) {
8568
let def_id = self.body.source.instance.def_id();
86-
if let Some(impl_def_id) = builtin_derive_def_id(self.tcx, def_id) {
69+
if let Some(impl_def_id) = self
70+
.tcx
71+
.impl_of_method(def_id)
72+
.filter(|&def_id| self.tcx.is_builtin_derive(def_id))
73+
{
8774
// If a method is defined in the local crate,
8875
// the impl containing that method should also be.
8976
self.tcx.ensure().unsafe_derive_on_repr_packed(impl_def_id.expect_local());

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ symbols! {
180180
Error,
181181
File,
182182
FileType,
183+
Fn,
184+
FnMut,
185+
FnOnce,
183186
FormatSpec,
184187
Formatter,
185188
From,
@@ -248,6 +251,7 @@ symbols! {
248251
RustcEncodable,
249252
Send,
250253
SeqCst,
254+
SliceIndex,
251255
Some,
252256
String,
253257
StructuralEq,

0 commit comments

Comments
 (0)