Skip to content

Commit 2451f12

Browse files
committed
Address review comments
1 parent 00eca69 commit 2451f12

File tree

6 files changed

+78
-90
lines changed

6 files changed

+78
-90
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
4444
}
4545
ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
4646
ExprKind::Call(ref f, ref args) => {
47-
if let Some(legacy_args) = self.legacy_const_generic_args(f) {
47+
if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
4848
self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
4949
} else {
5050
let f = self.lower_expr(f);
@@ -298,39 +298,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
298298
}
299299
}
300300

301-
/// Checks if an expression refers to a function marked with
302-
/// `#[rustc_legacy_const_generics]` and returns the argument index list
303-
/// from the attribute.
304-
fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>> {
305-
if let ExprKind::Path(None, path) = &expr.kind {
306-
if path.segments.last().unwrap().args.is_some() {
307-
return None;
308-
}
309-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
310-
if partial_res.unresolved_segments() != 0 {
311-
return None;
312-
}
313-
if let Res::Def(hir::def::DefKind::Fn, def_id) = partial_res.base_res() {
314-
let attrs = self.item_attrs(def_id);
315-
let attr = attrs
316-
.iter()
317-
.find(|a| self.sess.check_name(a, sym::rustc_legacy_const_generics))?;
318-
let mut ret = vec![];
319-
for meta in attr.meta_item_list()? {
320-
match meta.literal()?.kind {
321-
LitKind::Int(a, _) => {
322-
ret.push(a as usize);
323-
}
324-
_ => panic!("invalid arg index"),
325-
}
326-
}
327-
return Some(ret);
328-
}
329-
}
330-
}
331-
None
332-
}
333-
334301
fn lower_legacy_const_generics(
335302
&mut self,
336303
mut f: Expr,
@@ -366,12 +333,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
366333
}
367334
}
368335

369-
// Add generic args to the last element of the path
370-
path.segments.last_mut().unwrap().args =
371-
Some(AstP(GenericArgs::AngleBracketed(AngleBracketedArgs {
372-
span: DUMMY_SP,
373-
args: generic_args,
374-
})));
336+
// Add generic args to the last element of the path.
337+
let last_segment = path.segments.last_mut().unwrap();
338+
assert!(last_segment.args.is_none());
339+
last_segment.args = Some(AstP(GenericArgs::AngleBracketed(AngleBracketedArgs {
340+
span: DUMMY_SP,
341+
args: generic_args,
342+
})));
375343

376344
// Now lower everything as normal.
377345
let f = self.lower_expr(&f);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub trait ResolverAstLowering {
175175

176176
fn item_generics_num_lifetimes(&self, def: DefId, sess: &Session) -> usize;
177177

178-
fn item_attrs(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute>;
178+
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
179179

180180
/// Obtains resolution for a `NodeId` with a single resolution.
181181
fn get_partial_res(&mut self, id: NodeId) -> Option<PartialRes>;
@@ -2828,16 +2828,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
28282828
)
28292829
}
28302830
}
2831-
2832-
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> {
2833-
if let Some(_local_def_id) = def_id.as_local() {
2834-
// FIXME: This doesn't actually work, items doesn't include everything?
2835-
//self.items[&hir::ItemId { def_id: local_def_id }].attrs.into()
2836-
Vec::new()
2837-
} else {
2838-
self.resolver.item_attrs(def_id, self.sess)
2839-
}
2840-
}
28412831
}
28422832

28432833
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>) -> Vec<hir::BodyId> {

compiler/rustc_passes/src/check_attr.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,23 @@ impl CheckAttrVisitor<'tcx> {
789789
})) = item
790790
{
791791
let arg_count = decl.inputs.len() as u128 + generics.params.len() as u128;
792+
for param in generics.params {
793+
match param.kind {
794+
hir::GenericParamKind::Const { .. } => {}
795+
_ => {
796+
self.tcx
797+
.sess
798+
.struct_span_err(
799+
meta.span(),
800+
"#[rustc_legacy_const_generics] functions must \
801+
only have const generics",
802+
)
803+
.span_label(param.span, "non-const generic parameter")
804+
.emit();
805+
break;
806+
}
807+
}
808+
}
792809
if *val >= arg_count {
793810
let span = meta.span();
794811
self.tcx

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,8 +2326,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23262326

23272327
ExprKind::Call(ref callee, ref arguments) => {
23282328
self.resolve_expr(callee, Some(expr));
2329-
let const_args = self.legacy_const_generic_args(callee).unwrap_or(Vec::new());
2329+
let const_args = self.r.legacy_const_generic_args(callee).unwrap_or(Vec::new());
23302330
for (idx, argument) in arguments.iter().enumerate() {
2331+
// Constant arguments need to be treated as AnonConst since
2332+
// that is how they will be later lowered to HIR.
23312333
if const_args.contains(&idx) {
23322334
self.with_constant_rib(
23332335
IsRepeatExpr::No,
@@ -2418,42 +2420,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
24182420
Some((ident.name, ns)),
24192421
)
24202422
}
2421-
2422-
/// Checks if an expression refers to a function marked with
2423-
/// `#[rustc_legacy_const_generics]` and returns the argument index list
2424-
/// from the attribute.
2425-
fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>> {
2426-
if let ExprKind::Path(None, path) = &expr.kind {
2427-
if path.segments.last().unwrap().args.is_some() {
2428-
return None;
2429-
}
2430-
if let Some(partial_res) = self.r.get_partial_res(expr.id) {
2431-
if partial_res.unresolved_segments() != 0 {
2432-
return None;
2433-
}
2434-
if let Res::Def(def::DefKind::Fn, def_id) = partial_res.base_res() {
2435-
if def_id.is_local() {
2436-
return None;
2437-
}
2438-
let attrs = self.r.cstore().item_attrs(def_id, self.r.session);
2439-
let attr = attrs
2440-
.iter()
2441-
.find(|a| self.r.session.check_name(a, sym::rustc_legacy_const_generics))?;
2442-
let mut ret = vec![];
2443-
for meta in attr.meta_item_list()? {
2444-
match meta.literal()?.kind {
2445-
LitKind::Int(a, _) => {
2446-
ret.push(a as usize);
2447-
}
2448-
_ => panic!("invalid arg index"),
2449-
}
2450-
}
2451-
return Some(ret);
2452-
}
2453-
}
2454-
}
2455-
None
2456-
}
24572423
}
24582424

24592425
impl<'a> Resolver<'a> {

compiler/rustc_resolve/src/lib.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_ast::unwrap_or;
2929
use rustc_ast::visit::{self, Visitor};
3030
use rustc_ast::{self as ast, NodeId};
3131
use rustc_ast::{Crate, CRATE_NODE_ID};
32+
use rustc_ast::{Expr, ExprKind, LitKind};
3233
use rustc_ast::{ItemKind, ModKind, Path};
3334
use rustc_ast_lowering::ResolverAstLowering;
3435
use rustc_ast_pretty::pprust;
@@ -1076,8 +1077,8 @@ impl ResolverAstLowering for Resolver<'_> {
10761077
self.cstore().item_generics_num_lifetimes(def_id, sess)
10771078
}
10781079

1079-
fn item_attrs(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute> {
1080-
self.cstore().item_attrs(def_id, sess)
1080+
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
1081+
self.legacy_const_generic_args(expr)
10811082
}
10821083

10831084
fn get_partial_res(&mut self, id: NodeId) -> Option<PartialRes> {
@@ -3312,6 +3313,49 @@ impl<'a> Resolver<'a> {
33123313
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
33133314
if let Some(def_id) = def_id.as_local() { Some(self.def_id_to_span[def_id]) } else { None }
33143315
}
3316+
3317+
/// Checks if an expression refers to a function marked with
3318+
/// `#[rustc_legacy_const_generics]` and returns the argument index list
3319+
/// from the attribute.
3320+
pub fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
3321+
if let ExprKind::Path(None, path) = &expr.kind {
3322+
// Don't perform legacy const generics rewriting if the path already
3323+
// has generic arguments.
3324+
if path.segments.last().unwrap().args.is_some() {
3325+
return None;
3326+
}
3327+
3328+
let partial_res = self.partial_res_map.get(&expr.id)?;
3329+
if partial_res.unresolved_segments() != 0 {
3330+
return None;
3331+
}
3332+
3333+
if let Res::Def(def::DefKind::Fn, def_id) = partial_res.base_res() {
3334+
// We only support cross-crate argument rewriting. Uses
3335+
// within the same crate should be updated to use the new
3336+
// const generics style.
3337+
if def_id.is_local() {
3338+
return None;
3339+
}
3340+
3341+
let attrs = self.cstore().item_attrs(def_id, self.session);
3342+
let attr = attrs
3343+
.iter()
3344+
.find(|a| self.session.check_name(a, sym::rustc_legacy_const_generics))?;
3345+
let mut ret = vec![];
3346+
for meta in attr.meta_item_list()? {
3347+
match meta.literal()?.kind {
3348+
LitKind::Int(a, _) => {
3349+
ret.push(a as usize);
3350+
}
3351+
_ => panic!("invalid arg index"),
3352+
}
3353+
}
3354+
return Some(ret);
3355+
}
3356+
}
3357+
None
3358+
}
33153359
}
33163360

33173361
fn names_to_string(names: &[Symbol]) -> String {

src/test/ui/invalid/invalid-rustc_legacy_const_generics-arguments.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ extern {
2626
fn foo7(_: u8);
2727
}
2828

29+
#[rustc_legacy_const_generics(0)] //~ ERROR #[rustc_legacy_const_generics] functions must only have
30+
fn foo3<X>() {}
31+
2932
#[rustc_legacy_const_generics] //~ ERROR malformed `rustc_legacy_const_generics` attribute
3033
fn bar1() {}
3134

0 commit comments

Comments
 (0)