Skip to content

Commit 831abcd

Browse files
authored
Unrolled build for #152136
Rollup merge of #152136 - eggyal:consolidate-type-const-checks, r=BoxyUwU Consolidate type const checks on `tcx.is_type_const` A little bit of cleanup; explanation can be found in the reporting issue. Fixes #152124 r? BoxyUwU
2 parents cf16cd9 + 13141af commit 831abcd

File tree

8 files changed

+21
-30
lines changed

8 files changed

+21
-30
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
923923
);
924924
check_where_clauses(wfcx, def_id);
925925

926-
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
926+
if tcx.is_type_const(def_id.into()) {
927927
wfcheck::check_type_const(wfcx, def_id, ty, true)?;
928928
}
929929
Ok(())

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ use hir::def_id::{DefId, DefIdMap, LocalDefId};
66
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
77
use rustc_errors::codes::*;
88
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, pluralize, struct_span_code_err};
9-
use rustc_hir::attrs::AttributeKind;
109
use rustc_hir::def::{DefKind, Res};
1110
use rustc_hir::intravisit::VisitorExt;
12-
use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, find_attr, intravisit};
11+
use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, intravisit};
1312
use rustc_infer::infer::{self, BoundRegionConversionTime, InferCtxt, TyCtxtInferExt};
1413
use rustc_infer::traits::util;
1514
use rustc_middle::ty::error::{ExpectedFound, TypeError};
@@ -2051,12 +2050,8 @@ fn compare_type_const<'tcx>(
20512050
impl_const_item: ty::AssocItem,
20522051
trait_const_item: ty::AssocItem,
20532052
) -> Result<(), ErrorGuaranteed> {
2054-
let impl_is_type_const =
2055-
find_attr!(tcx.get_all_attrs(impl_const_item.def_id), AttributeKind::TypeConst(_));
2056-
let trait_type_const_span = find_attr!(
2057-
tcx.get_all_attrs(trait_const_item.def_id),
2058-
AttributeKind::TypeConst(sp) => *sp
2059-
);
2053+
let impl_is_type_const = tcx.is_type_const(impl_const_item.def_id);
2054+
let trait_type_const_span = tcx.type_const_span(trait_const_item.def_id);
20602055

20612056
if let Some(trait_type_const_span) = trait_type_const_span
20622057
&& !impl_is_type_const

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ pub(crate) fn check_associated_item(
953953
wfcx.register_wf_obligation(span, loc, ty.into());
954954

955955
let has_value = item.defaultness(tcx).has_value();
956-
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
956+
if tcx.is_type_const(def_id.into()) {
957957
check_type_const(wfcx, def_id, ty, has_value)?;
958958
}
959959

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
44
use rustc_errors::codes::*;
55
use rustc_errors::struct_span_code_err;
66
use rustc_hir as hir;
7-
use rustc_hir::attrs::AttributeKind;
7+
use rustc_hir::PolyTraitRef;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
10-
use rustc_hir::{PolyTraitRef, find_attr};
1110
use rustc_middle::bug;
1211
use rustc_middle::ty::{
1312
self as ty, IsSuggestable, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
@@ -603,10 +602,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
603602
});
604603

605604
if let ty::AssocTag::Const = assoc_tag
606-
&& !find_attr!(
607-
self.tcx().get_all_attrs(assoc_item.def_id),
608-
AttributeKind::TypeConst(_)
609-
)
605+
&& !self.tcx().is_type_const(assoc_item.def_id)
610606
{
611607
if tcx.features().min_generic_const_args() {
612608
let mut err = self.dcx().struct_span_err(

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ use rustc_errors::codes::*;
2828
use rustc_errors::{
2929
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
3030
};
31-
use rustc_hir::attrs::AttributeKind;
3231
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
3332
use rustc_hir::def_id::{DefId, LocalDefId};
34-
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId, find_attr};
33+
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
3534
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3635
use rustc_infer::traits::DynCompatibilityViolation;
3736
use rustc_macros::{TypeFoldable, TypeVisitable};
@@ -1423,7 +1422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14231422
LowerTypeRelativePathMode::Const,
14241423
)? {
14251424
TypeRelativePath::AssocItem(def_id, args) => {
1426-
if !find_attr!(self.tcx().get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
1425+
if !self.tcx().is_type_const(def_id) {
14271426
let mut err = self.dcx().struct_span_err(
14281427
span,
14291428
"use of trait associated const without `#[type_const]`",
@@ -1896,7 +1895,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18961895
ty::AssocTag::Const,
18971896
) {
18981897
Ok((item_def_id, item_args)) => {
1899-
if !find_attr!(self.tcx().get_all_attrs(item_def_id), AttributeKind::TypeConst(_)) {
1898+
if !self.tcx().is_type_const(item_def_id) {
19001899
let mut err = self.dcx().struct_span_err(
19011900
span,
19021901
"use of `const` in the type system without `#[type_const]`",

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ mod variance;
8585

8686
pub use errors::NoVariantNamed;
8787
use rustc_abi::{CVariadicStatus, ExternAbi};
88-
use rustc_hir::attrs::AttributeKind;
88+
use rustc_hir as hir;
8989
use rustc_hir::def::DefKind;
9090
use rustc_hir::lints::DelayedLint;
91-
use rustc_hir::{
92-
find_attr, {self as hir},
93-
};
9491
use rustc_middle::mir::interpret::GlobalId;
9592
use rustc_middle::query::Providers;
9693
use rustc_middle::ty::{Const, Ty, TyCtxt};
@@ -238,7 +235,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
238235
}
239236
DefKind::Const
240237
if !tcx.generics_of(item_def_id).own_requires_monomorphization()
241-
&& !find_attr!(tcx.get_all_attrs(item_def_id), AttributeKind::TypeConst(_)) =>
238+
&& !tcx.is_type_const(item_def_id.into()) =>
242239
{
243240
// FIXME(generic_const_items): Passing empty instead of identity args is fishy but
244241
// seems to be fine for now. Revisit this!

compiler/rustc_middle/src/ty/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,10 +1886,15 @@ impl<'tcx> TyCtxt<'tcx> {
18861886
self.is_lang_item(self.parent(def_id), LangItem::AsyncDropInPlace)
18871887
}
18881888

1889+
pub fn type_const_span(self, def_id: DefId) -> Option<Span> {
1890+
matches!(self.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
1891+
.then(|| find_attr!(self.get_all_attrs(def_id), AttributeKind::TypeConst(sp) => *sp))
1892+
.flatten()
1893+
}
1894+
18891895
/// Check if the given `def_id` is a const with the `#[type_const]` attribute.
18901896
pub fn is_type_const(self, def_id: DefId) -> bool {
1891-
matches!(self.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
1892-
&& find_attr!(self.get_all_attrs(def_id), AttributeKind::TypeConst(_))
1897+
self.type_const_span(def_id).is_some()
18931898
}
18941899

18951900
/// Returns the movability of the coroutine of `def_id`, or panics

compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
use std::ops::ControlFlow;
88

99
use rustc_errors::FatalError;
10-
use rustc_hir::attrs::AttributeKind;
1110
use rustc_hir::def::DefKind;
1211
use rustc_hir::def_id::DefId;
13-
use rustc_hir::{self as hir, LangItem, find_attr};
12+
use rustc_hir::{self as hir, LangItem};
1413
use rustc_middle::query::Providers;
1514
use rustc_middle::ty::{
1615
self, EarlyBinder, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
@@ -333,7 +332,7 @@ pub fn dyn_compatibility_violations_for_assoc_item(
333332
if tcx.features().min_generic_const_args() {
334333
if !tcx.generics_of(item.def_id).is_own_empty() {
335334
errors.push(AssocConstViolation::Generic);
336-
} else if !find_attr!(tcx.get_all_attrs(item.def_id), AttributeKind::TypeConst(_)) {
335+
} else if !tcx.is_type_const(item.def_id) {
337336
errors.push(AssocConstViolation::NonType);
338337
}
339338

0 commit comments

Comments
 (0)