Skip to content

Commit

Permalink
add is_host_effect to GenericParamDefKind::Const and address review
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Sep 11, 2023
1 parent 84a4907 commit 9654d5c
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
Ty::new_misc_error(tcx).into()
}
}
GenericParamDefKind::Const { has_default } => {
GenericParamDefKind::Const { has_default, .. } => {
let ty = tcx
.at(self.span)
.type_of(param.def_id)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id

let is_our_default = |def: &ty::GenericParamDef| match def.kind {
GenericParamDefKind::Type { has_default, .. }
| GenericParamDefKind::Const { has_default } => {
| GenericParamDefKind::Const { has_default, .. } => {
has_default && def.index >= generics.parent_count as u32
}
GenericParamDefKind::Lifetime => unreachable!(),
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
name: param.name.ident().name,
def_id: param.def_id.to_def_id(),
pure_wrt_drop: param.pure_wrt_drop,
kind: ty::GenericParamDefKind::Const { has_default: default.is_some() },
kind: ty::GenericParamDefKind::Const {
has_default: default.is_some(),
is_host_effect: is_host_param,
},
})
}
}));
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,10 +1295,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
self.fcx.ty_infer(Some(param), inf.span).into()
}
(&GenericParamDefKind::Const { has_default }, GenericArg::Infer(inf)) => {
(
&GenericParamDefKind::Const { has_default, is_host_effect },
GenericArg::Infer(inf),
) => {
let tcx = self.fcx.tcx();

if has_default && tcx.has_attr(param.def_id, sym::rustc_host) {
if has_default && is_host_effect {
self.fcx.var_for_effect(param)
} else {
self.fcx
Expand Down Expand Up @@ -1341,7 +1344,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.fcx.var_for_def(self.span, param)
}
}
GenericParamDefKind::Const { has_default } => {
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 All @@ -1352,7 +1355,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// it before falling back to default, such that a `const fn` such as
// `needs_drop::<()>` can still be called in const contexts. (if we defaulted
// instead of inferred, typeck would error)
if tcx.has_attr(param.def_id, sym::rustc_host) {
if is_host_effect {
return self.fcx.var_for_effect(param);
} else if !infer_args {
return tcx
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt};
use rustc_session::Session;
use rustc_span::symbol::Ident;
use rustc_span::{self, sym, Span, DUMMY_SP};
use rustc_span::{self, Span, DUMMY_SP};
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode, ObligationCtxt};

use std::cell::{Cell, RefCell};
Expand Down Expand Up @@ -268,9 +268,12 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
) -> Const<'tcx> {
// FIXME ideally this shouldn't use unwrap
match param {
Some(param) if self.tcx.has_attr(param.def_id, sym::rustc_host) => {
self.var_for_effect(param).as_const().unwrap()
}
Some(
param @ ty::GenericParamDef {
kind: ty::GenericParamDefKind::Const { is_host_effect: true, .. },
..
},
) => self.var_for_effect(param).as_const().unwrap(),
Some(param) => self.var_for_def(span, param).as_const().unwrap(),
None => self.next_const_var(
ty,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,9 @@ fn float_unification_error<'tcx>(
}

fn effect_unification_error<'tcx>(
tcx: TyCtxt<'tcx>,
a_is_expected: bool,
(a, b): (EffectVarValue<'tcx>, EffectVarValue<'tcx>),
_tcx: TyCtxt<'tcx>,
_a_is_expected: bool,
(_a, _b): (EffectVarValue<'tcx>, EffectVarValue<'tcx>),
) -> TypeError<'tcx> {
TypeError::ConstMismatch(ExpectedFound::new(a_is_expected, a.as_const(tcx), b.as_const(tcx)))
bug!("unexpected effect unification error")
}
7 changes: 3 additions & 4 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtx
use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid};
use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef};
use rustc_span::symbol::Symbol;
use rustc_span::{sym, Span};
use rustc_span::Span;

use std::cell::{Cell, RefCell};
use std::fmt;
Expand Down Expand Up @@ -1181,9 +1181,8 @@ impl<'tcx> InferCtxt<'tcx> {

Ty::new_var(self.tcx, ty_var_id).into()
}
GenericParamDefKind::Const { .. } => {
// todo what about using effect var here
if self.tcx.has_attr(param.def_id, sym::rustc_host) {
GenericParamDefKind::Const { is_host_effect, .. } => {
if is_host_effect {
return self.var_for_effect(param);
}
let origin = ConstVariableOrigin {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,14 +873,14 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
| DefKind::AssocConst
| DefKind::Macro(_)
| DefKind::Field
| DefKind::Impl { .. }
| DefKind::ConstParam => true,
| DefKind::Impl { .. } => true,
// Tools may want to be able to detect their tool lints on
// closures from upstream crates, too. This is used by
// https://github.com/model-checking/kani and is not a performance
// or maintenance issue for us.
DefKind::Closure => true,
DefKind::TyParam
| DefKind::ConstParam
| DefKind::Ctor(..)
| DefKind::ExternCrate
| DefKind::Use
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ impl<'tcx> CanonicalVarValues<'tcx> {
};
ty::Region::new_late_bound(tcx, ty::INNERMOST, br).into()
}
// todo eh?
CanonicalVarKind::Effect => ty::Const::new_bound(
tcx,
ty::INNERMOST,
Expand Down
6 changes: 3 additions & 3 deletions 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, EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamT
pub enum GenericParamDefKind {
Lifetime,
Type { has_default: bool, synthetic: bool },
Const { has_default: bool },
Const { has_default: bool, is_host_effect: bool },
}

impl GenericParamDefKind {
Expand Down Expand Up @@ -87,7 +87,7 @@ impl GenericParamDef {
GenericParamDefKind::Type { has_default, .. } if has_default => {
Some(tcx.type_of(self.def_id).map_bound(|t| t.into()))
}
GenericParamDefKind::Const { has_default } if has_default => {
GenericParamDefKind::Const { has_default, .. } if has_default => {
Some(tcx.const_param_default(self.def_id).map_bound(|c| c.into()))
}
_ => None,
Expand Down Expand Up @@ -187,7 +187,7 @@ impl<'tcx> Generics {
GenericParamDefKind::Type { has_default, .. } => {
own_defaults.types += has_default as usize;
}
GenericParamDefKind::Const { has_default } => {
GenericParamDefKind::Const { has_default, .. } => {
own_defaults.consts += has_default as usize;
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, '_> {
self.visit(self.ev.tcx.type_of(param.def_id).instantiate_identity());
}
}
GenericParamDefKind::Const { has_default } => {
GenericParamDefKind::Const { has_default, .. } => {
self.visit(self.ev.tcx.type_of(param.def_id).instantiate_identity());
if has_default {
self.visit(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,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 } => {
ty::GenericParamDefKind::Const { has_default, is_host_effect: _ } => {
GenericParamDefKind::Const { has_default: *has_default }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ fn clean_generic_param_def<'tcx>(
},
)
}
ty::GenericParamDefKind::Const { has_default } => (
ty::GenericParamDefKind::Const { has_default, .. } => (
def.name,
GenericParamDefKind::Const {
ty: Box::new(clean_middle_ty(
Expand Down

0 comments on commit 9654d5c

Please sign in to comment.