Index: flang/lib/Evaluate/fold-character.cpp =================================================================== --- flang/lib/Evaluate/fold-character.cpp +++ flang/lib/Evaluate/fold-character.cpp @@ -56,7 +56,13 @@ if (name == "achar" || name == "char") { using IntT = SubscriptInteger; return FoldElementalIntrinsic(context, std::move(funcRef), - ScalarFunc([](const Scalar &i) { + ScalarFunc([&](const Scalar &i) { + if (i.IsNegative() || i.BGE(Scalar{0}.IBSET(8 * KIND))) { + context.messages().Say( + "%s(I=%jd) is out of range for CHARACTER(KIND=%d)"_warn_en_US, + parser::ToUpperCaseLetters(name), + static_cast(i.ToInt64()), KIND); + } return CharacterUtils::CHAR(i.ToUInt64()); })); } else if (name == "adjustl") { Index: flang/test/Evaluate/errors01.f90 =================================================================== --- flang/test/Evaluate/errors01.f90 +++ flang/test/Evaluate/errors01.f90 @@ -100,6 +100,32 @@ !CHECK: error: DIM=4 argument to SPREAD must be between 1 and 3 integer, parameter :: bad3 = spread(matrix, 4, 1) end subroutine + subroutine s10 + !CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=1) + character(kind=1), parameter :: badc11 = char(-1,kind=1) + !CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=1) + character(kind=1), parameter :: bada11 = achar(-1,kind=1) + !CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=2) + character(kind=2), parameter :: badc21 = char(-1,kind=2) + !CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=2) + character(kind=2), parameter :: bada21 = achar(-1,kind=2) + !CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=4) + character(kind=4), parameter :: badc41 = char(-1,kind=4) + !CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=4) + character(kind=4), parameter :: bada41 = achar(-1,kind=4) + !CHECK: warning: CHAR(I=256) is out of range for CHARACTER(KIND=1) + character(kind=1), parameter :: badc12 = char(256,kind=1) + !CHECK: warning: ACHAR(I=256) is out of range for CHARACTER(KIND=1) + character(kind=1), parameter :: bada12 = achar(256,kind=1) + !CHECK: warning: CHAR(I=65536) is out of range for CHARACTER(KIND=2) + character(kind=2), parameter :: badc22 = char(65536,kind=2) + !CHECK: warning: ACHAR(I=65536) is out of range for CHARACTER(KIND=2) + character(kind=2), parameter :: bada22 = achar(65536,kind=2) + !CHECK: warning: CHAR(I=4294967296) is out of range for CHARACTER(KIND=4) + character(kind=4), parameter :: badc42 = char(4294967296_8,kind=4) + !CHECK: warning: ACHAR(I=4294967296) is out of range for CHARACTER(KIND=4) + character(kind=4), parameter :: bada42 = achar(4294967296_8,kind=4) + end subroutine subroutine warnings real, parameter :: ok1 = scale(0.0, 99999) ! 0.0 real, parameter :: ok2 = scale(1.0, -99999) ! 0.0 Index: flang/test/Evaluate/folding05.f90 =================================================================== --- flang/test/Evaluate/folding05.f90 +++ flang/test/Evaluate/folding05.f90 @@ -11,7 +11,9 @@ logical, parameter :: test_char1_ok2 = char(127_4, 1).EQ."" logical, parameter :: test_char1_ok3 = char(97_4, 1).EQ."a" logical, parameter :: test_char1_ok4 = .NOT.char(97_4, 1).EQ."b" + !WARN: warning: CHAR(I=355) is out of range for CHARACTER(KIND=1) logical, parameter :: test_char1_ok5 = char(355_4, 1).EQ."c" + !WARN: warning: CHAR(I=-61) is out of range for CHARACTER(KIND=1) logical, parameter :: test_char1_ok6 = char(-61_1, 1).EQ.char(195_4, 1) logical, parameter :: test_char2_ok1 = char(0_4, 2).EQ.2_"" @@ -21,6 +23,7 @@ logical, parameter :: test_char4_ok1 = char(0, 4).EQ.4_"" logical, parameter :: test_char4_ok2 = char(INT(12435, 4), 4).EQ.4_"ん" logical, parameter :: test_char4_ok3 = char(354_4, 4).EQ.4_"Ţ" + !WARN: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=4) logical, parameter :: test_char4_ok4 = char(-1_1, 4).EQ.char(x'ffffffff', 4) character(kind=4, len=*), parameter :: c4aok(*) = char([97_4, 98_4, 99_4, 20320_4, 22909_4], 4) @@ -38,15 +41,19 @@ logical, parameter :: test_ichar1 = char(ichar("a")).EQ."a" logical, parameter :: test_ichar2 = ichar(char(255)).EQ.255 !WARN: warning: Result of intrinsic function 'ichar' (255) overflows its result type + !WARN: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=1) logical, parameter :: test_ichar3 = ichar(char(-1_1), 1).EQ.-1_1 logical, parameter :: test_ichar4 = ichar(char(2147483647_4, 4), 4).EQ.2147483647_4 logical, parameter :: test_ichar5 = ichar(char(4294967295_8, 4), 8).EQ.4294967295_8 + !WARN: warning: CHAR(I=4294967296) is out of range for CHARACTER(KIND=4) logical, parameter :: test_ichar6 = ichar(char(4294967296_8, 4), 8).EQ.0_8 logical, parameter :: test_iachar1 = achar(iachar("a")).EQ."a" logical, parameter :: test_iachar2 = iachar(achar(22)).EQ.22 !WARN: warning: Result of intrinsic function 'ichar' (2147483649) overflows its result type + !WARN: warning: CHAR(I=-2147483647) is out of range for CHARACTER(KIND=4) logical, parameter :: test_iachar3 = ichar(char(-2147483647_4, 4), 4).EQ.(-2147483647_4) logical, parameter :: test_iachar5 = ichar(char(65535_4, 2), 4).EQ.65535_4 + !WARN: warning: CHAR(I=65536) is out of range for CHARACTER(KIND=2) logical, parameter :: test_iachar6 = ichar(char(65536_4, 2), 4).EQ.0_4 ! Not yet recognized as intrinsic