diff --git a/.gitattributes b/.gitattributes
index e9c484a43f..2c93e176a4 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -18444,6 +18444,7 @@ tests/webtbs/tw37554.pp svneol=native#text/pascal
 tests/webtbs/tw3758.pp svneol=native#text/plain
 tests/webtbs/tw3764.pp svneol=native#text/plain
 tests/webtbs/tw3765.pp svneol=native#text/plain
+tests/webtbs/tw37650.pp svneol=native#text/pascal
 tests/webtbs/tw3768.pp svneol=native#text/plain
 tests/webtbs/tw3774.pp svneol=native#text/plain
 tests/webtbs/tw3777.pp svneol=native#text/plain
diff --git a/compiler/pexpr.pas b/compiler/pexpr.pas
index 9b0757b2d0..852889a35d 100644
--- a/compiler/pexpr.pas
+++ b/compiler/pexpr.pas
@@ -114,7 +114,12 @@ implementation
                end
              else
                begin
-                if (tordconstnode(p).value<=0) then
+                { the node is a generic param while parsing a generic def
+                  so disable the range checking for the string }
+                if parse_generic and
+                  (nf_generic_para in p.flags) then
+                  tordconstnode(p).value:=255;
+                if tordconstnode(p).value<=0 then
                   begin
                      Message(parser_e_invalid_string_size);
                      tordconstnode(p).value:=255;
diff --git a/tests/webtbs/tw37650.pp b/tests/webtbs/tw37650.pp
new file mode 100644
index 0000000000..b769c0ed9e
--- /dev/null
+++ b/tests/webtbs/tw37650.pp
@@ -0,0 +1,22 @@
+{ %NORUN }
+
+program tw37650;
+
+{$mode objfpc}
+
+type
+  generic TMyClass<const U: Integer> = class
+    type TKey = String[U];
+  end;
+
+generic procedure Test<const U: Integer>;
+type
+  TKey = String[U];
+begin
+end;
+
+type
+  TMyClass12 = specialize TMyClass<12>;
+begin
+  specialize Test<12>;
+end.