Skip to content

Commit 84937e1

Browse files
committed
Gate mgca codepath for inherent consts under inherent_associated_types
1 parent 7d221d5 commit 84937e1

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ use rustc_middle::ty::{
4444
};
4545
use rustc_middle::{bug, span_bug};
4646
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
47+
use rustc_session::parse::feature_err;
4748
use rustc_span::edit_distance::find_best_match_for_name;
48-
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw};
49+
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
4950
use rustc_trait_selection::infer::InferCtxtExt;
5051
use rustc_trait_selection::traits::wf::object_region_bounds;
5152
use rustc_trait_selection::traits::{self, ObligationCtxt};
@@ -1167,11 +1168,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11671168
)? {
11681169
LoweredAssoc::Term(def_id, args) => {
11691170
let assoc = tcx.associated_item(def_id);
1170-
let ty = if matches!(assoc, ty::AssocItem {
1171-
container: ty::AssocItemContainer::Impl,
1172-
trait_item_def_id: None,
1173-
..
1174-
}) {
1171+
let ty = if matches!(
1172+
assoc,
1173+
ty::AssocItem {
1174+
container: ty::AssocItemContainer::Impl,
1175+
trait_item_def_id: None,
1176+
..
1177+
}
1178+
) {
11751179
Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new_from_args(tcx, def_id, args))
11761180
} else {
11771181
Ty::new_projection_from_args(tcx, def_id, args)
@@ -1472,14 +1476,29 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14721476
) -> Result<Option<(DefId, GenericArgsRef<'tcx>)>, ErrorGuaranteed> {
14731477
let tcx = self.tcx();
14741478

1475-
// Don't attempt to look up inherent associated types when the feature is not enabled.
1476-
// Theoretically it'd be fine to do so since we feature-gate their definition site.
1477-
// However, due to current limitations of the implementation (caused by us performing
1478-
// selection during HIR ty lowering instead of in the trait solver), IATs can lead to cycle
1479-
// errors (#108491) which mask the feature-gate error, needlessly confusing users
1480-
// who use IATs by accident (#113265).
1481-
if kind == ty::AssocKind::Type && !tcx.features().inherent_associated_types() {
1482-
return Ok(None);
1479+
if !tcx.features().inherent_associated_types() {
1480+
match kind {
1481+
// Don't attempt to look up inherent associated types when the feature is not enabled.
1482+
// Theoretically it'd be fine to do so since we feature-gate their definition site.
1483+
// However, due to current limitations of the implementation (caused by us performing
1484+
// selection during HIR ty lowering instead of in the trait solver), IATs can lead to cycle
1485+
// errors (#108491) which mask the feature-gate error, needlessly confusing users
1486+
// who use IATs by accident (#113265).
1487+
ty::AssocKind::Type => return Ok(None),
1488+
ty::AssocKind::Const => {
1489+
// We also gate the mgca codepath for type-level uses of inherent consts
1490+
// with the inherent_associated_types feature gate since it relies on the
1491+
// same machinery and has similar rough edges.
1492+
return Err(feature_err(
1493+
&tcx.sess,
1494+
sym::inherent_associated_types,
1495+
span,
1496+
"inherent associated types are unstable",
1497+
)
1498+
.emit());
1499+
}
1500+
ty::AssocKind::Fn => unreachable!(),
1501+
}
14831502
}
14841503

14851504
let name = segment.ident;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(min_generic_const_args)]
2+
#![allow(incomplete_features)]
3+
4+
struct S;
5+
6+
impl S {
7+
const N: usize = 42;
8+
}
9+
10+
fn main() {
11+
let _x: [(); S::N] = todo!();
12+
//~^ ERROR inherent associated types are unstable
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: inherent associated types are unstable
2+
--> $DIR/inherent-const-gating.rs:11:18
3+
|
4+
LL | let _x: [(); S::N] = todo!();
5+
| ^^^^
6+
|
7+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
8+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)