Skip to content

Commit 6ed469c

Browse files
committed
IAT: Reinstate early bailout
1 parent 843f8ce commit 6ed469c

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
@@ -1481,11 +1481,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14811481
match assoc_tag {
14821482
// Don't attempt to look up inherent associated types when the feature is not
14831483
// enabled. Theoretically it'd be fine to do so since we feature-gate their
1484-
// definition site. However, due to current limitations of the implementation
1485-
// (caused by us performing selection during HIR ty lowering instead of in the
1486-
// trait solver), IATs can lead to cycle errors (#108491) which mask the
1487-
// feature-gate error, needlessly confusing users who use IATs by accident
1488-
// (#113265).
1484+
// definition site. However, the current implementation of inherent associated
1485+
// items is somewhat brittle, so let's not run it by default.
14891486
ty::AssocTag::Type => return Ok(None),
14901487
ty::AssocTag::Const => {
14911488
// We also gate the mgca codepath for type-level uses of inherent consts
@@ -1514,9 +1511,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15141511
})
15151512
.collect();
15161513

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

1525+
// FIXME(#142006): Don't eagerly error here, there might be applicable trait candidates.
15201526
let InherentAssocCandidate { impl_, assoc_item, scope: def_scope } =
15211527
match &applicable_candidates[..] {
15221528
&[] => Err(self.report_unresolved_inherent_assoc_item(
@@ -1537,6 +1543,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15371543
)),
15381544
}?;
15391545

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

15421550
// 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)