@@ -476,3 +476,100 @@ fn minus_belongs_to_literal() {
476476 --"# ] ] ,
477477 ) ;
478478}
479+
480+ #[ test]
481+ fn negative_literals_in_const_generics ( ) {
482+ // Test that negative literals work correctly in declarative macros
483+ // when used as const generic arguments. The issue was that expressions
484+ // like -1 would be wrapped in parentheses, creating invalid syntax
485+ // Foo::<(-1)> instead of the correct Foo::<-1>.
486+ let decl = r#"
487+ ($val:expr) => {
488+ struct Foo<const I: i16> {
489+ pub value: i16,
490+ }
491+
492+ impl<const I: i16> Foo<I> {
493+ pub fn new(value: i16) -> Self {
494+ Self { value }
495+ }
496+ }
497+
498+ Foo::<$val>::new($val)
499+ };
500+ "# ;
501+ let check = |args, expect| check ( Edition :: CURRENT , Edition :: CURRENT , decl, args, expect) ;
502+
503+ // Test negative integer literal - should produce Foo::<-1>, not Foo::<(-1)>
504+ check (
505+ "-1" ,
506+ expect ! [ [ r#"
507+ SUBTREE $$ 1:Root[0000, 0]@0..2#ROOT2024 1:Root[0000, 0]@0..2#ROOT2024
508+ IDENT struct 0:Root[0000, 0]@22..28#ROOT2024
509+ IDENT Foo 0:Root[0000, 0]@29..32#ROOT2024
510+ PUNCH < [alone] 0:Root[0000, 0]@32..33#ROOT2024
511+ IDENT const 0:Root[0000, 0]@33..38#ROOT2024
512+ IDENT I 0:Root[0000, 0]@39..40#ROOT2024
513+ PUNCH : [alone] 0:Root[0000, 0]@40..41#ROOT2024
514+ IDENT i16 0:Root[0000, 0]@42..45#ROOT2024
515+ PUNCH > [alone] 0:Root[0000, 0]@45..46#ROOT2024
516+ SUBTREE {} 0:Root[0000, 0]@47..48#ROOT2024 0:Root[0000, 0]@77..78#ROOT2024
517+ IDENT pub 0:Root[0000, 0]@57..60#ROOT2024
518+ IDENT value 0:Root[0000, 0]@61..66#ROOT2024
519+ PUNCH : [alone] 0:Root[0000, 0]@66..67#ROOT2024
520+ IDENT i16 0:Root[0000, 0]@68..71#ROOT2024
521+ PUNCH , [alone] 0:Root[0000, 0]@71..72#ROOT2024
522+ IDENT impl 0:Root[0000, 0]@84..88#ROOT2024
523+ PUNCH < [alone] 0:Root[0000, 0]@88..89#ROOT2024
524+ IDENT const 0:Root[0000, 0]@89..94#ROOT2024
525+ IDENT I 0:Root[0000, 0]@95..96#ROOT2024
526+ PUNCH : [alone] 0:Root[0000, 0]@96..97#ROOT2024
527+ IDENT i16 0:Root[0000, 0]@98..101#ROOT2024
528+ PUNCH > [alone] 0:Root[0000, 0]@101..102#ROOT2024
529+ IDENT Foo 0:Root[0000, 0]@103..106#ROOT2024
530+ PUNCH < [alone] 0:Root[0000, 0]@106..107#ROOT2024
531+ IDENT I 0:Root[0000, 0]@107..108#ROOT2024
532+ PUNCH > [alone] 0:Root[0000, 0]@108..109#ROOT2024
533+ SUBTREE {} 0:Root[0000, 0]@110..111#ROOT2024 0:Root[0000, 0]@194..195#ROOT2024
534+ IDENT pub 0:Root[0000, 0]@120..123#ROOT2024
535+ IDENT fn 0:Root[0000, 0]@124..126#ROOT2024
536+ IDENT new 0:Root[0000, 0]@127..130#ROOT2024
537+ SUBTREE () 0:Root[0000, 0]@130..131#ROOT2024 0:Root[0000, 0]@141..142#ROOT2024
538+ IDENT value 0:Root[0000, 0]@131..136#ROOT2024
539+ PUNCH : [alone] 0:Root[0000, 0]@136..137#ROOT2024
540+ IDENT i16 0:Root[0000, 0]@138..141#ROOT2024
541+ PUNCH - [joint] 0:Root[0000, 0]@143..144#ROOT2024
542+ PUNCH > [alone] 0:Root[0000, 0]@144..145#ROOT2024
543+ IDENT Self 0:Root[0000, 0]@146..150#ROOT2024
544+ SUBTREE {} 0:Root[0000, 0]@151..152#ROOT2024 0:Root[0000, 0]@188..189#ROOT2024
545+ IDENT Self 0:Root[0000, 0]@165..169#ROOT2024
546+ SUBTREE {} 0:Root[0000, 0]@170..171#ROOT2024 0:Root[0000, 0]@178..179#ROOT2024
547+ IDENT value 0:Root[0000, 0]@172..177#ROOT2024
548+ IDENT Foo 0:Root[0000, 0]@201..204#ROOT2024
549+ PUNCH : [joint] 0:Root[0000, 0]@204..205#ROOT2024
550+ PUNCH : [joint] 0:Root[0000, 0]@205..206#ROOT2024
551+ PUNCH < [joint] 0:Root[0000, 0]@206..207#ROOT2024
552+ PUNCH - [alone] 1:Root[0000, 0]@0..1#ROOT2024
553+ LITERAL Integer 1 1:Root[0000, 0]@1..2#ROOT2024
554+ PUNCH > [joint] 0:Root[0000, 0]@211..212#ROOT2024
555+ PUNCH : [joint] 0:Root[0000, 0]@212..213#ROOT2024
556+ PUNCH : [alone] 0:Root[0000, 0]@213..214#ROOT2024
557+ IDENT new 0:Root[0000, 0]@214..217#ROOT2024
558+ SUBTREE () 0:Root[0000, 0]@217..218#ROOT2024 0:Root[0000, 0]@222..223#ROOT2024
559+ PUNCH - [alone] 1:Root[0000, 0]@0..1#ROOT2024
560+ LITERAL Integer 1 1:Root[0000, 0]@1..2#ROOT2024
561+
562+ struct Foo<const I:i16>{
563+ pub value:i16,
564+ }
565+ impl <const I:i16>Foo<I>{
566+ pub fn new(value:i16) -> Self {
567+ Self {
568+ value
569+ }
570+ }
571+
572+ }
573+ Foo::<-1>::new(-1)"# ] ] ,
574+ ) ;
575+ }
0 commit comments