Skip to content

Commit 870a01a

Browse files
committed
Auto merge of rust-lang#120558 - oli-obk:missing_impl_item_ice, r=estebank
Stop bailing out from compilation just because there were incoherent traits fixes rust-lang#120343 but also has a lot of "type annotations needed" fallout. Some are fixed in the second commit.
2 parents 384b02c + a59a1e7 commit 870a01a

File tree

50 files changed

+490
-146
lines changed

Some content is hidden

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

50 files changed

+490
-146
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,10 @@ pub(super) fn check_type_bounds<'tcx>(
19901990
impl_ty: ty::AssocItem,
19911991
impl_trait_ref: ty::TraitRef<'tcx>,
19921992
) -> Result<(), ErrorGuaranteed> {
1993+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
1994+
// other `Foo` impls are incoherent.
1995+
tcx.ensure().coherent_trait(impl_trait_ref.def_id)?;
1996+
19931997
let param_env = tcx.param_env(impl_ty.def_id);
19941998
debug!(?param_env);
19951999

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,11 @@ fn check_associated_item(
10051005
enter_wf_checking_ctxt(tcx, span, item_id, |wfcx| {
10061006
let item = tcx.associated_item(item_id);
10071007

1008+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
1009+
// other `Foo` impls are incoherent.
1010+
tcx.ensure()
1011+
.coherent_trait(tcx.parent(item.trait_item_def_id.unwrap_or(item_id.into())))?;
1012+
10081013
let self_ty = match item.container {
10091014
ty::TraitContainer => tcx.types.self_param,
10101015
ty::ImplContainer => tcx.type_of(item.container_id(tcx)).instantiate_identity(),
@@ -1291,6 +1296,9 @@ fn check_impl<'tcx>(
12911296
// therefore don't need to be WF (the trait's `Self: Trait` predicate
12921297
// won't hold).
12931298
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
1299+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
1300+
// other `Foo` impls are incoherent.
1301+
tcx.ensure().coherent_trait(trait_ref.def_id)?;
12941302
let trait_ref = wfcx.normalize(
12951303
ast_trait_ref.path.span,
12961304
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
169169

170170
tcx.sess.time("coherence_checking", || {
171171
// Check impls constrain their parameters
172-
let mut res =
172+
let res =
173173
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_impl_wf(module));
174174

175175
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
176-
res = res.and(tcx.ensure().coherent_trait(trait_def_id));
176+
let _ = tcx.ensure().coherent_trait(trait_def_id);
177177
}
178178
// these queries are executed for side-effects (error reporting):
179179
res.and(tcx.ensure().crate_inherent_impls(()))

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn check_legal_trait_for_method_call(
4141
receiver: Option<Span>,
4242
expr_span: Span,
4343
trait_id: DefId,
44-
) {
44+
) -> Result<(), ErrorGuaranteed> {
4545
if tcx.lang_items().drop_trait() == Some(trait_id) {
4646
let sugg = if let Some(receiver) = receiver.filter(|s| !s.is_empty()) {
4747
errors::ExplicitDestructorCallSugg::Snippet {
@@ -51,8 +51,9 @@ pub fn check_legal_trait_for_method_call(
5151
} else {
5252
errors::ExplicitDestructorCallSugg::Empty(span)
5353
};
54-
tcx.dcx().emit_err(errors::ExplicitDestructorCall { span, sugg });
54+
return Err(tcx.dcx().emit_err(errors::ExplicitDestructorCall { span, sugg }));
5555
}
56+
tcx.coherent_trait(trait_id)
5657
}
5758

5859
#[derive(Debug)]

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,13 +1105,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11051105
let container_id = assoc_item.container_id(tcx);
11061106
debug!(?def_id, ?container, ?container_id);
11071107
match container {
1108-
ty::TraitContainer => callee::check_legal_trait_for_method_call(
1109-
tcx,
1110-
path_span,
1111-
None,
1112-
span,
1113-
container_id,
1114-
),
1108+
ty::TraitContainer => {
1109+
if let Err(e) = callee::check_legal_trait_for_method_call(
1110+
tcx,
1111+
path_span,
1112+
None,
1113+
span,
1114+
container_id,
1115+
) {
1116+
self.set_tainted_by_errors(e);
1117+
}
1118+
}
11151119
ty::ImplContainer => {
11161120
if segments.len() == 1 {
11171121
// `<T>::assoc` will end up here, and so

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,15 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
630630
fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
631631
// Disallow calls to the method `drop` defined in the `Drop` trait.
632632
if let Some(trait_def_id) = pick.item.trait_container(self.tcx) {
633-
callee::check_legal_trait_for_method_call(
633+
if let Err(e) = callee::check_legal_trait_for_method_call(
634634
self.tcx,
635635
self.span,
636636
Some(self.self_expr.span),
637637
self.call_expr.span,
638638
trait_def_id,
639-
)
639+
) {
640+
self.set_tainted_by_errors(e);
641+
}
640642
}
641643
}
642644

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23712371
return e;
23722372
}
23732373

2374+
if let Err(guar) = self.tcx.ensure().coherent_trait(trait_ref.def_id()) {
2375+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
2376+
// other `Foo` impls are incoherent.
2377+
return guar;
2378+
}
2379+
23742380
// This is kind of a hack: it frequently happens that some earlier
23752381
// error prevents types from being fully inferred, and then we get
23762382
// a bunch of uninteresting errors saying something like "<generic
@@ -2666,6 +2672,14 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26662672
if let Some(e) = self.tainted_by_errors() {
26672673
return e;
26682674
}
2675+
2676+
if let Err(guar) =
2677+
self.tcx.ensure().coherent_trait(self.tcx.parent(data.projection_ty.def_id))
2678+
{
2679+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
2680+
// other `Foo` impls are incoherent.
2681+
return guar;
2682+
}
26692683
let subst = data
26702684
.projection_ty
26712685
.args

library/core/src/primitive_docs.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -448,22 +448,6 @@ mod prim_unit {}
448448
#[doc(hidden)]
449449
impl () {}
450450

451-
// Fake impl that's only really used for docs.
452-
#[cfg(doc)]
453-
#[stable(feature = "rust1", since = "1.0.0")]
454-
impl Clone for () {
455-
fn clone(&self) -> Self {
456-
loop {}
457-
}
458-
}
459-
460-
// Fake impl that's only really used for docs.
461-
#[cfg(doc)]
462-
#[stable(feature = "rust1", since = "1.0.0")]
463-
impl Copy for () {
464-
// empty
465-
}
466-
467451
#[rustc_doc_primitive = "pointer"]
468452
#[doc(alias = "ptr")]
469453
#[doc(alias = "*")]
@@ -1690,23 +1674,3 @@ mod prim_fn {}
16901674
// See src/librustdoc/passes/collect_trait_impls.rs:collect_trait_impls
16911675
#[doc(hidden)]
16921676
impl<Ret, T> fn(T) -> Ret {}
1693-
1694-
// Fake impl that's only really used for docs.
1695-
#[cfg(doc)]
1696-
#[stable(feature = "rust1", since = "1.0.0")]
1697-
#[doc(fake_variadic)]
1698-
/// This trait is implemented on function pointers with any number of arguments.
1699-
impl<Ret, T> Clone for fn(T) -> Ret {
1700-
fn clone(&self) -> Self {
1701-
loop {}
1702-
}
1703-
}
1704-
1705-
// Fake impl that's only really used for docs.
1706-
#[cfg(doc)]
1707-
#[stable(feature = "rust1", since = "1.0.0")]
1708-
#[doc(fake_variadic)]
1709-
/// This trait is implemented on function pointers with any number of arguments.
1710-
impl<Ret, T> Copy for fn(T) -> Ret {
1711-
// empty
1712-
}

tests/ui/associated-consts/issue-105330.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ fn foo<A: TraitWAssocConst<A=32>>() { //~ ERROR E0658
1414

1515
fn main<A: TraitWAssocConst<A=32>>() {
1616
//~^ ERROR E0658
17+
//~| ERROR E0131
1718
foo::<Demo>();
1819
}

tests/ui/associated-consts/issue-105330.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ LL | impl TraitWAssocConst for impl Demo {
4343
|
4444
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
4545

46-
error: aborting due to 5 previous errors
46+
error[E0131]: `main` function is not allowed to have generic parameters
47+
--> $DIR/issue-105330.rs:15:8
48+
|
49+
LL | fn main<A: TraitWAssocConst<A=32>>() {
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` cannot have generic parameters
51+
52+
error: aborting due to 6 previous errors
4753

48-
Some errors have detailed explanations: E0404, E0562, E0658.
49-
For more information about an error, try `rustc --explain E0404`.
54+
Some errors have detailed explanations: E0131, E0404, E0562, E0658.
55+
For more information about an error, try `rustc --explain E0131`.

0 commit comments

Comments
 (0)