Skip to content

Commit 24e0724

Browse files
authored
Rollup merge of rust-lang#148771 - fmease:iat-reinstate-early-elim, r=BoxyUwU
IAT: Reinstate early bailout Apparently, some people are already using IATs in their projects and get blocked by rust-lang#142006 (comment) (cc dupes rust-lang#143952 & rust-lang#148535). Since the (temporary) fix is so trivial, let's just do it. Addresses rust-lang#142006 (comment). cc ```@luissantosHCIT``` (rust-lang#148535). r? ```@BoxyUwU```
2 parents d738f03 + 6ed469c commit 24e0724

File tree

6 files changed

+66
-66
lines changed

6 files changed

+66
-66
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,11 +1487,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14871487
match assoc_tag {
14881488
// Don't attempt to look up inherent associated types when the feature is not
14891489
// enabled. Theoretically it'd be fine to do so since we feature-gate their
1490-
// definition site. However, due to current limitations of the implementation
1491-
// (caused by us performing selection during HIR ty lowering instead of in the
1492-
// trait solver), IATs can lead to cycle errors (#108491) which mask the
1493-
// feature-gate error, needlessly confusing users who use IATs by accident
1494-
// (#113265).
1490+
// definition site. However, the current implementation of inherent associated
1491+
// items is somewhat brittle, so let's not run it by default.
14951492
ty::AssocTag::Type => return Ok(None),
14961493
ty::AssocTag::Const => {
14971494
// We also gate the mgca codepath for type-level uses of inherent consts
@@ -1520,9 +1517,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15201517
})
15211518
.collect();
15221519

1520+
// At the moment, we actually bail out with a hard error if the selection of an inherent
1521+
// associated item fails (see below). This means we never consider trait associated items
1522+
// as potential fallback candidates (#142006). To temporarily mask that issue, let's not
1523+
// select at all if there are no early inherent candidates.
1524+
if candidates.is_empty() {
1525+
return Ok(None);
1526+
}
1527+
15231528
let (applicable_candidates, fulfillment_errors) =
15241529
self.select_inherent_assoc_candidates(span, self_ty, candidates.clone());
15251530

1531+
// FIXME(#142006): Don't eagerly error here, there might be applicable trait candidates.
15261532
let InherentAssocCandidate { impl_, assoc_item, scope: def_scope } =
15271533
match &applicable_candidates[..] {
15281534
&[] => Err(self.report_unresolved_inherent_assoc_item(
@@ -1543,6 +1549,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15431549
)),
15441550
}?;
15451551

1552+
// FIXME(#142006): Don't eagerly validate here, there might be trait candidates that are
1553+
// accessible (visible and stable) contrary to the inherent candidate.
15461554
self.check_assoc_item(assoc_item, name, def_scope, block, span);
15471555

15481556
// FIXME(fmease): Currently creating throwaway `parent_args` to please
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Ensure that IAT selection doesn't hard error on associated type paths that could refer to
2+
// an inherent associated type if we can't find an applicable inherent candidate since there
3+
// might still be valid trait associated type candidates.
4+
//
5+
// FIXME(#142006): This only covers the bare minimum, we also need to disqualify inherent
6+
// candidates if they're inaccessible or if the impl headers don't match / apply.
7+
//
8+
// issue: <https://github.com/rust-lang/rust/issues/142006#issuecomment-2938846613>
9+
//@ check-pass
10+
11+
#![feature(inherent_associated_types)]
12+
#![expect(incomplete_features)]
13+
14+
struct Type;
15+
trait Trait { type AssocTy; fn scope(); }
16+
17+
impl Trait for Type {
18+
type AssocTy = ();
19+
20+
fn scope() {
21+
let (): Self::AssocTy;
22+
}
23+
}
24+
25+
fn main() { <Type as Trait>::scope(); }

tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.shadowed.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.uncovered.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(inherent_associated_types)]
2+
#![expect(incomplete_features)]
3+
4+
// Ensure that prefer inherent associated types over trait associated types
5+
// (assuming the impl headers match and they're accessible).
6+
//@ check-pass
7+
8+
struct Adt;
9+
10+
impl Adt {
11+
type Ty = ();
12+
}
13+
14+
trait Trait {
15+
type Ty;
16+
fn scope();
17+
}
18+
19+
impl Trait for Adt {
20+
type Ty = i32;
21+
fn scope() {
22+
// We prefer the inherent assoc ty `Adt::Ty` (`()`) over the
23+
// trait assoc ty `<Adt as Trait>::Ty` (`i32`).
24+
let (): Self::Ty;
25+
}
26+
}
27+
28+
fn main() {}

0 commit comments

Comments
 (0)