Skip to content

Commit

Permalink
Merge 9d6aa97 into 865eaf9
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Jun 3, 2024
2 parents 865eaf9 + 9d6aa97 commit 1014099
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 44 deletions.
13 changes: 9 additions & 4 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{DiagArgFromDisplay, DiagCtxt, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir::{
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
};
Expand Down Expand Up @@ -1185,7 +1185,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
})
});
return GenericArg::Const(ConstArg {
value: ct,
kind: ConstArgKind::Anon(ct),
is_desugared_from_effects: false,
});
}
Expand All @@ -1196,7 +1196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericArg::Type(self.lower_ty(ty, itctx))
}
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
value: self.lower_anon_const(ct),
kind: ConstArgKind::Anon(self.lower_anon_const(ct)),
is_desugared_from_effects: false,
}),
}
Expand Down Expand Up @@ -2654,7 +2654,12 @@ impl<'hir> GenericArgsCtor<'hir> {

lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
self.args.push(hir::GenericArg::Const(hir::ConstArg {
value: lcx.arena.alloc(hir::AnonConst { def_id, hir_id, body, span }),
kind: hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
def_id,
hir_id,
body,
span,
})),
is_desugared_from_effects: true,
}))
}
Expand Down
25 changes: 22 additions & 3 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,30 @@ impl<'hir> PathSegment<'hir> {

#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct ConstArg<'hir> {
pub value: &'hir AnonConst,
pub kind: ConstArgKind<'hir>,
/// Indicates whether this comes from a `~const` desugaring.
pub is_desugared_from_effects: bool,
}

impl<'hir> ConstArg<'hir> {
pub fn span(&self) -> Span {
match self.kind {
ConstArgKind::Anon(anon) => anon.span,
}
}

pub fn hir_id(&self) -> HirId {
match self.kind {
ConstArgKind::Anon(anon) => anon.hir_id,
}
}
}

#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub enum ConstArgKind<'hir> {
Anon(&'hir AnonConst),
}

#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct InferArg {
pub hir_id: HirId,
Expand All @@ -260,7 +279,7 @@ impl GenericArg<'_> {
match self {
GenericArg::Lifetime(l) => l.ident.span,
GenericArg::Type(t) => t.span,
GenericArg::Const(c) => c.value.span,
GenericArg::Const(c) => c.span(),
GenericArg::Infer(i) => i.span,
}
}
Expand All @@ -269,7 +288,7 @@ impl GenericArg<'_> {
match self {
GenericArg::Lifetime(l) => l.hir_id,
GenericArg::Type(t) => t.hir_id,
GenericArg::Const(c) => c.value.hir_id,
GenericArg::Const(c) => c.hir_id(),
GenericArg::Infer(i) => i.hir_id,
}
}
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ pub trait Visitor<'v>: Sized {
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
walk_anon_const(self, c)
}
fn visit_const_arg(&mut self, c: &'v ConstArg<'v>) -> Self::Result {
walk_const_arg(self, c)
}
fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
walk_expr(self, ex)
}
Expand Down Expand Up @@ -716,6 +719,15 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
visitor.visit_nested_body(constant.body)
}

pub fn walk_const_arg<'v, V: Visitor<'v>>(
visitor: &mut V,
const_arg: &'v ConstArg<'v>,
) -> V::Result {
match const_arg.kind {
ConstArgKind::Anon(anon) => visitor.visit_anon_const(anon),
}
}

pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
try_visit!(visitor.visit_id(expression.hir_id));
match expression.kind {
Expand Down Expand Up @@ -1203,7 +1215,7 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(
match generic_arg {
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
GenericArg::Type(ty) => visitor.visit_ty(ty),
GenericArg::Const(ct) => visitor.visit_anon_const(&ct.value),
GenericArg::Const(ct) => visitor.visit_const_arg(ct),
GenericArg::Infer(inf) => visitor.visit_infer(inf),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
i += 1;
}
GenericArg::Const(ct) => {
self.visit_anon_const(&ct.value);
self.visit_const_arg(ct);
i += 1;
}
GenericArg::Infer(inf) => {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use rustc_ast::ast::ParamKindOrd;
use rustc_errors::{
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan,
};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::GenericArg;
use rustc_hir::{self as hir, ConstArgKind};
use rustc_middle::ty::{
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
};
Expand Down Expand Up @@ -112,7 +112,8 @@ fn generic_arg_mismatch_err(
}
}
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
let body = tcx.hir().body(cnst.value.body);
let ConstArgKind::Anon(anon) = cnst.kind;
let body = tcx.hir().body(anon.body);
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
{
if let Res::Def(DefKind::Fn { .. }, id) = path.res {
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_errors::{
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, FatalError,
};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{walk_generics, Visitor as _};
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir::{GenericArg, GenericArgs, HirId};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
Expand Down Expand Up @@ -479,11 +479,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
handle_ty_args(has_default, &inf.to_ty())
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
let did = ct.value.def_id;
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
ty::Const::from_anon_const(tcx, did).into()
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match &ct.kind {
ConstArgKind::Anon(anon) => {
let did = anon.def_id;
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
ty::Const::from_anon_const(tcx, did).into()
}
},
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
let ty = tcx
.at(self.span)
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
use rustc_ast_pretty::pp::{self, Breaks};
use rustc_ast_pretty::pprust::{Comments, PrintState};
use rustc_hir as hir;
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir::{
BindingMode, ByRef, GenericArg, GenericBound, GenericParam, GenericParamKind, HirId,
LifetimeParamKind, Node, PatKind, RangeEnd, Term, TraitBoundModifier,
Expand Down Expand Up @@ -982,6 +982,12 @@ impl<'a> State<'a> {
self.ann.nested(self, Nested::Body(constant.body))
}

fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
match &const_arg.kind {
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
}
}

fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
self.popen();
self.commasep_exprs(Inconsistent, args);
Expand Down Expand Up @@ -1670,7 +1676,7 @@ impl<'a> State<'a> {
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
GenericArg::Lifetime(_) => {}
GenericArg::Type(ty) => s.print_type(ty),
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
GenericArg::Const(ct) => s.print_const_arg(ct),
GenericArg::Infer(_inf) => s.word("_"),
}
});
Expand Down
32 changes: 20 additions & 12 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::rvalue_scopes;
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{self as hir, ConstArg, ConstArgKind};
use rustc_hir::{ExprKind, GenericArg, HirId, Node, QPath};
use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
use rustc_hir_analysis::hir_ty_lowering::generics::{
Expand Down Expand Up @@ -446,16 +446,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

pub fn lower_const_arg(&self, hir_ct: &hir::AnonConst, param_def_id: DefId) -> ty::Const<'tcx> {
let did = hir_ct.def_id;
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
let ct = ty::Const::from_anon_const(self.tcx, did);
self.register_wf_obligation(
ct.into(),
self.tcx.hir().span(hir_ct.hir_id),
ObligationCauseCode::WellFormed(None),
);
ct
pub fn lower_const_arg(
&self,
const_arg: &ConstArg<'tcx>,
param_def_id: DefId,
) -> ty::Const<'tcx> {
match &const_arg.kind {
ConstArgKind::Anon(anon) => {
let did = anon.def_id;
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
let ct = ty::Const::from_anon_const(self.tcx, did);
self.register_wf_obligation(
ct.into(),
self.tcx.hir().span(anon.hir_id),
ObligationCauseCode::WellFormed(None),
);
ct
}
}
}

// If the type given by the user has free regions, save it for later, since
Expand Down Expand Up @@ -1283,7 +1291,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.fcx.lower_ty(ty).raw.into()
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
self.fcx.lower_const_arg(&ct.value, param.def_id).into()
self.fcx.lower_const_arg(ct, param.def_id).into()
}
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
self.fcx.ty_infer(Some(param), inf.span).into()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
self.cfcx.lower_ty(ty).raw.into()
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
self.cfcx.lower_const_arg(&ct.value, param.def_id).into()
self.cfcx.lower_const_arg(ct, param.def_id).into()
}
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
self.cfcx.ty_infer(Some(param), inf.span).into()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
.tcx
.sess
.source_map()
.span_to_snippet(c.value.span)
.span_to_snippet(c.span())
.unwrap_or_else(|_| "_".into()),
GenericArg::Infer(_) => String::from("_"),
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/hir_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
match ga {
hir::GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
hir::GenericArg::Type(ty) => self.visit_ty(ty),
hir::GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
hir::GenericArg::Const(ct) => self.visit_const_arg(ct),
hir::GenericArg::Infer(inf) => self.visit_infer(inf),
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
use rustc_errors::{codes::*, struct_span_code_err, FatalError};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::PredicateOrigin;
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir_analysis::lower_ty;
use rustc_middle::metadata::Reexport;
use rustc_middle::middle::resolve_bound_vars as rbv;
Expand Down Expand Up @@ -282,10 +282,16 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
}

pub(crate) fn clean_const<'tcx>(
constant: &hir::ConstArg<'_>,
constant: &hir::ConstArg<'tcx>,
cx: &mut DocContext<'tcx>,
) -> Constant {
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
match &constant.kind {
ConstArgKind::Anon(anon) => clean_anon_const(anon, cx),
}
}

fn clean_anon_const<'tcx>(anon: &'tcx hir::AnonConst, cx: &mut DocContext<'tcx>) -> Constant {
let def_id = cx.tcx.hir().body_owner_def_id(anon.body).to_def_id();
Constant {
type_: Box::new(clean_middle_ty(
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
Expand All @@ -294,7 +300,7 @@ pub(crate) fn clean_const<'tcx>(
None,
)),
generics: Generics::default(),
kind: ConstantKind::Anonymous { body: constant.value.body },
kind: ConstantKind::Anonymous { body: anon.body },
}
}

Expand Down
Loading

0 comments on commit 1014099

Please sign in to comment.