Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5348a89

Browse files
committedFeb 13, 2023
Auto merge of rust-lang#108015 - matthiaskrgr:rollup-qerohjn, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#107902 (fix: improve the suggestion on future not awaited) - rust-lang#107913 (Update broken link in cargo style guide) - rust-lang#107942 (Tighter spans for bad inherent `impl` self types) - rust-lang#107948 (Allow shortcuts to directories to be used for ./x.py fmt) - rust-lang#107971 (Clearly document intentional UB in mir-opt tests) - rust-lang#107985 (Added another error to be processed in fallback) - rust-lang#108002 (Update books) - rust-lang#108013 (rustdoc: use a string with one-character codes for search index types) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 065852d + f1a3494 commit 5348a89

File tree

51 files changed

+322
-269
lines changed

Some content is hidden

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

51 files changed

+322
-269
lines changed
 

‎compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const ADD_ATTR: &str =
5757
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
5858

5959
impl<'tcx> InherentCollect<'tcx> {
60-
fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId) {
60+
fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId, span: Span) {
6161
let impl_def_id = item.owner_id;
6262
if let Some(def_id) = def_id.as_local() {
6363
// Add the implementation to the mapping from implementation to base
@@ -76,12 +76,12 @@ impl<'tcx> InherentCollect<'tcx> {
7676
if !self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
7777
struct_span_err!(
7878
self.tcx.sess,
79-
item.span,
79+
span,
8080
E0390,
8181
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
8282
)
8383
.help(INTO_DEFINING_CRATE)
84-
.span_help(item.span, ADD_ATTR_TO_TY)
84+
.span_help(span, ADD_ATTR_TO_TY)
8585
.emit();
8686
return;
8787
}
@@ -93,12 +93,12 @@ impl<'tcx> InherentCollect<'tcx> {
9393
{
9494
struct_span_err!(
9595
self.tcx.sess,
96-
item.span,
96+
span,
9797
E0390,
9898
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
9999
)
100100
.help(INTO_DEFINING_CRATE)
101-
.span_help(impl_item.span, ADD_ATTR)
101+
.span_help(self.tcx.hir().span(impl_item.id.hir_id()), ADD_ATTR)
102102
.emit();
103103
return;
104104
}
@@ -112,12 +112,12 @@ impl<'tcx> InherentCollect<'tcx> {
112112
} else {
113113
struct_span_err!(
114114
self.tcx.sess,
115-
item.span,
115+
span,
116116
E0116,
117117
"cannot define inherent `impl` for a type outside of the crate \
118118
where the type is defined"
119119
)
120-
.span_label(item.span, "impl for type defined outside of crate.")
120+
.span_label(span, "impl for type defined outside of crate.")
121121
.note("define and implement a trait or new type instead")
122122
.emit();
123123
}
@@ -182,29 +182,30 @@ impl<'tcx> InherentCollect<'tcx> {
182182
}
183183

184184
let item = self.tcx.hir().item(id);
185-
let hir::ItemKind::Impl(hir::Impl { of_trait: None, self_ty: ty, items, .. }) = item.kind else {
185+
let impl_span = self.tcx.hir().span(id.hir_id());
186+
let hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) = item.kind else {
186187
return;
187188
};
188189

189190
let self_ty = self.tcx.type_of(item.owner_id);
190191
match *self_ty.kind() {
191192
ty::Adt(def, _) => {
192-
self.check_def_id(item, self_ty, def.did());
193+
self.check_def_id(item, self_ty, def.did(), impl_span);
193194
}
194195
ty::Foreign(did) => {
195-
self.check_def_id(item, self_ty, did);
196+
self.check_def_id(item, self_ty, did, impl_span);
196197
}
197198
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
198-
self.check_def_id(item, self_ty, data.principal_def_id().unwrap());
199+
self.check_def_id(item, self_ty, data.principal_def_id().unwrap(), impl_span);
199200
}
200201
ty::Dynamic(..) => {
201202
struct_span_err!(
202203
self.tcx.sess,
203-
ty.span,
204+
impl_span,
204205
E0785,
205206
"cannot define inherent `impl` for a dyn auto trait"
206207
)
207-
.span_label(ty.span, "impl requires at least one non-auto trait")
208+
.span_label(impl_span, "impl requires at least one non-auto trait")
208209
.note("define and implement a new trait or type instead")
209210
.emit();
210211
}
@@ -221,17 +222,17 @@ impl<'tcx> InherentCollect<'tcx> {
221222
| ty::Never
222223
| ty::FnPtr(_)
223224
| ty::Tuple(..) => {
224-
self.check_primitive_impl(item.owner_id.def_id, self_ty, items, ty.span)
225+
self.check_primitive_impl(item.owner_id.def_id, self_ty, items, impl_span)
225226
}
226227
ty::Alias(..) | ty::Param(_) => {
227228
let mut err = struct_span_err!(
228229
self.tcx.sess,
229-
ty.span,
230+
impl_span,
230231
E0118,
231232
"no nominal type found for inherent implementation"
232233
);
233234

234-
err.span_label(ty.span, "impl requires a nominal type")
235+
err.span_label(impl_span, "impl requires a nominal type")
235236
.note("either implement a trait on it or create a newtype to wrap it instead");
236237

237238
err.emit();

‎compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,14 +1783,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
17831783
}
17841784
}))
17851785
{
1786-
diag.note_expected_found_extra(
1787-
&expected_label,
1788-
expected,
1789-
&found_label,
1790-
found,
1791-
&sort_string(values.expected, exp_p),
1792-
&sort_string(values.found, found_p),
1793-
);
1786+
if let Some(ExpectedFound { found: found_ty, .. }) = exp_found {
1787+
// `Future` is a special opaque type that the compiler
1788+
// will try to hide in some case such as `async fn`, so
1789+
// to make an error more use friendly we will
1790+
// avoid to suggest a mismatch type with a
1791+
// type that the user usually are not usign
1792+
// directly such as `impl Future<Output = u8>`.
1793+
if !self.tcx.ty_is_opaque_future(found_ty) {
1794+
diag.note_expected_found_extra(
1795+
&expected_label,
1796+
expected,
1797+
&found_label,
1798+
found,
1799+
&sort_string(values.expected, exp_p),
1800+
&sort_string(values.found, found_p),
1801+
);
1802+
}
1803+
}
17941804
}
17951805
}
17961806
_ => {
@@ -2854,6 +2864,7 @@ impl IntoDiagnosticArg for ObligationCauseAsDiagArg<'_> {
28542864
pub enum TyCategory {
28552865
Closure,
28562866
Opaque,
2867+
OpaqueFuture,
28572868
Generator(hir::GeneratorKind),
28582869
Foreign,
28592870
}
@@ -2863,6 +2874,7 @@ impl TyCategory {
28632874
match self {
28642875
Self::Closure => "closure",
28652876
Self::Opaque => "opaque type",
2877+
Self::OpaqueFuture => "future",
28662878
Self::Generator(gk) => gk.descr(),
28672879
Self::Foreign => "foreign type",
28682880
}
@@ -2871,7 +2883,11 @@ impl TyCategory {
28712883
pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
28722884
match *ty.kind() {
28732885
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2874-
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => Some((Self::Opaque, def_id)),
2886+
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
2887+
let kind =
2888+
if tcx.ty_is_opaque_future(ty) { Self::OpaqueFuture } else { Self::Opaque };
2889+
Some((kind, def_id))
2890+
}
28752891
ty::Generator(def_id, ..) => {
28762892
Some((Self::Generator(tcx.generator_kind(def_id).unwrap()), def_id))
28772893
}

0 commit comments

Comments
 (0)