Skip to content

Commit

Permalink
Mark effects marker traits' runtime params as is_host_param.
Browse files Browse the repository at this point in the history
This avoids mix up when we create inference params for them and try to equate those to effect inference params.
  • Loading branch information
fee1-dead committed Feb 10, 2024
1 parent 046b67b commit 02e547e
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 20 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,12 +1596,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
}),
)),
)),
// FIXME(effects) we might not need a default.
default: Some(hir::AnonConst {
def_id: anon_const,
hir_id: const_id,
body: const_body,
}),
is_host_effect: true,
synthetic: true,
},
colon_span: None,
pure_wrt_drop: false,
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
param: &GenericParam,
source: hir::GenericParamSource,
) -> hir::GenericParam<'hir> {
let (name, kind) = self.lower_generic_param_kind(param, source);
let (name, kind) = self.lower_generic_param_kind(
param,
source,
attr::contains_name(&param.attrs, sym::rustc_runtime),
);

let hir_id = self.lower_node_id(param.id);
self.lower_attrs(hir_id, &param.attrs);
Expand All @@ -2132,6 +2136,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
param: &GenericParam,
source: hir::GenericParamSource,
is_host_effect: bool,
) -> (hir::ParamName, hir::GenericParamKind<'hir>) {
match &param.kind {
GenericParamKind::Lifetime => {
Expand Down Expand Up @@ -2197,7 +2202,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

(
hir::ParamName::Plain(self.lower_ident(param.ident)),
hir::GenericParamKind::Const { ty, default, is_host_effect: false },
hir::GenericParamKind::Const { ty, default, is_host_effect, synthetic: false },
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_const_eval/src/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
hir::Constness::Const
}
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }) => tcx
.generics_of(def_id)
.host_effect_index
.associated_type_for_effects(def_id)
.map_or(hir::Constness::NotConst, |_| hir::Constness::Const),
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_attr!(
rustc_const_panic_str, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
),
rustc_attr!(rustc_runtime, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),

// ==========================================================================
// Internal attributes, Layout related:
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ pub enum GenericParamKind<'hir> {
/// Optional default value for the const generic param
default: Option<AnonConst>,
is_host_effect: bool,
synthetic: bool,
},
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
match param.kind {
GenericParamKind::Lifetime { .. } => {}
GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
GenericParamKind::Const { ref ty, ref default, is_host_effect: _ } => {
GenericParamKind::Const { ref ty, ref default, is_host_effect: _, synthetic: _ } => {
visitor.visit_ty(ty);
if let Some(ref default) = default {
visitor.visit_const_param_default(param.hir_id, default);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ pub(crate) fn check_generic_arg_count(
.params
.iter()
.filter(|param| {
matches!(param.kind, ty::GenericParamDefKind::Const { is_host_effect: true, .. })
matches!(param.kind, ty::GenericParamDefKind::Const { synthetic: true, .. })
})
.count();
let named_const_param_count = param_counts.consts - synth_const_param_count;
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_hir_analysis/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ impl<'tcx> Bounds<'tcx> {
tcx.impl_trait_ref(defining_def_id).unwrap().instantiate_identity()
};
// create a new projection type `<T as TraitForBound>::Effects`
let assoc = tcx.associated_type_for_effects(trait_ref.def_id()).unwrap();
let Some(assoc) = tcx.associated_type_for_effects(trait_ref.def_id()) else {
tcx.dcx().span_delayed_bug(
span,
"`~const` trait bound has no effect assoc yet no errors encountered?",
);
return;
};
let self_ty = Ty::new_projection(tcx, assoc, trait_ref.skip_binder().args);
// we might have `~const Tr` where `Tr` isn't a `#[const_trait]`.
let Some(assoc_def) = tcx.associated_type_for_effects(trait_we_are_in.def_id)
else {
tcx.dcx().span_delayed_bug(
span,
"`~const` bound trait has no effect param yet no errors encountered?",
"`~const` trait bound has no effect assoc yet no errors encountered?",
);
return;
};
Expand All @@ -104,7 +110,13 @@ impl<'tcx> Bounds<'tcx> {
}
} {
// create a new projection type `<T as Tr>::Effects`
let assoc = tcx.associated_type_for_effects(trait_ref.def_id()).unwrap();
let Some(assoc) = tcx.associated_type_for_effects(trait_ref.def_id()) else {
tcx.dcx().span_delayed_bug(
span,
"`~const` trait bound has no effect assoc yet no errors encountered?",
);
return;
};
let self_ty = Ty::new_projection(tcx, assoc, trait_ref.skip_binder().args);
// make `<T as Tr>::Effects: Compat<runtime>`
let new_trait_ref = ty::TraitRef::new(
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,12 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => Ok(()),

// Const parameters are well formed if their type is structural match.
hir::GenericParamKind::Const { ty: hir_ty, default: _, is_host_effect: _ } => {
hir::GenericParamKind::Const {
ty: hir_ty,
default: _,
is_host_effect: _,
synthetic: _,
} => {
let ty = tcx.type_of(param.def_id).instantiate_identity();

if tcx.features().adt_const_params {
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
kind,
})
}
GenericParamKind::Const { ty: _, default, is_host_effect } => {
GenericParamKind::Const { ty: _, default, is_host_effect, synthetic } => {
if !matches!(allow_defaults, Defaults::Allowed)
&& default.is_some()
// `host` effect params are allowed to have defaults.
Expand Down Expand Up @@ -332,6 +332,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
kind: ty::GenericParamDefKind::Const {
has_default: default.is_some(),
is_host_effect,
synthetic,
},
})
}
Expand Down Expand Up @@ -504,7 +505,8 @@ struct AnonConstInParamTyDetector {

impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
if let GenericParamKind::Const { ty, default: _, is_host_effect: _ } = p.kind {
if let GenericParamKind::Const { ty, default: _, is_host_effect: _, synthetic: _ } = p.kind
{
let prev = self.in_param_ty;
self.in_param_ty = true;
self.visit_ty(ty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
self.visit_ty(ty);
}
}
GenericParamKind::Const { ty, default, is_host_effect: _ } => {
GenericParamKind::Const { ty, default, .. } => {
self.visit_ty(ty);
if let Some(default) = default {
self.visit_body(self.tcx.hir().body(default.body));
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ impl<'a> State<'a> {
self.print_type(default);
}
}
GenericParamKind::Const { ty, ref default, is_host_effect: _ } => {
GenericParamKind::Const { ty, ref default, is_host_effect: _, synthetic: _ } => {
self.word_space(":");
self.print_type(ty);
if let Some(default) = default {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.fcx.ty_infer(Some(param), inf.span).into()
}
(
&GenericParamDefKind::Const { has_default, is_host_effect },
&GenericParamDefKind::Const { has_default, is_host_effect, .. },
GenericArg::Infer(inf),
) => {
let tcx = self.fcx.tcx();
Expand Down Expand Up @@ -1349,7 +1349,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.fcx.var_for_def(self.span, param)
}
}
GenericParamDefKind::Const { has_default, is_host_effect } => {
GenericParamDefKind::Const { has_default, is_host_effect, .. } => {
if has_default {
// N.B. this is a bit of a hack. `infer_args` is passed depending on
// whether the user has provided generic args. E.g. for `Vec::new`
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt};
pub enum GenericParamDefKind {
Lifetime,
Type { has_default: bool, synthetic: bool },
Const { has_default: bool, is_host_effect: bool },
Const { has_default: bool, is_host_effect: bool, synthetic: bool },
}

impl GenericParamDefKind {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/convert/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {
ty::GenericParamDefKind::Type { has_default, synthetic } => {
GenericParamDefKind::Type { has_default: *has_default, synthetic: *synthetic }
}
ty::GenericParamDefKind::Const { has_default, is_host_effect: _ } => {
ty::GenericParamDefKind::Const { has_default, is_host_effect: _, synthetic: _ } => {
GenericParamDefKind::Const { has_default: *has_default }
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ symbols! {
rustc_reallocator,
rustc_regions,
rustc_reservation_impl,
rustc_runtime,
rustc_safe_intrinsic,
rustc_serialize,
rustc_skip_array_during_method_dispatch,
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,11 @@ pub mod effects {
pub struct Runtime;

#[lang = "EffectsCompat"]
pub trait Compat<const RUNTIME: bool> {}
pub trait Compat<#[rustc_runtime] const RUNTIME: bool = true> {}

impl Compat<false> for NoRuntime {}
impl Compat<true> for Runtime {}
impl<const RUNTIME: bool> Compat<RUNTIME> for Maybe {}
impl<#[rustc_runtime] const RUNTIME: bool> Compat<RUNTIME> for Maybe {}

#[lang = "EffectsEq"]
pub trait EffectsEq<T: ?Sized> {}
Expand Down

0 comments on commit 02e547e

Please sign in to comment.