Repository: spack/spack

Issue:
Compiler version constraint is not properly accounted with multiple compilers
### Steps to reproduce

Using this `compilers.yaml`:
```yaml
compilers:
- compiler:
    spec: gcc@11.0.0
    paths:
      cc: /usr/bin/gcc
      cxx: /usr/bin/g++
      f77: /usr/bin/gfortran
      fc: /usr/bin/gfortran
    flags: {}
    operating_system: ubuntu22.10
    target: x86_64
    modules: []
    environment: {}
    extra_rpaths: []
- compiler:
    spec: gcc@12.2.0
    paths:
      cc: /usr/bin/gcc
      cxx: /usr/bin/g++
      f77: /usr/bin/gfortran
      fc: /usr/bin/gfortran
    flags: {}
    operating_system: ubuntu22.10
    target: x86_64
    modules: []
    environment: {}
    extra_rpaths: []
```

### Error message

```console
$ spack spec  zlib%gcc@:11
Input spec
--------------------------------
zlib%gcc@:11

Concretized
--------------------------------
zlib@1.2.13%gcc@12.2.0+optimize+pic+shared build_system=makefile arch=linux-ubuntu20.04-icelake
```

### Information on your system

* **Spack:** 0.20.0.dev0 (3d597e29bee1429d2b8d204016df40b575900d66)
* **Python:** 3.8.10
* **Platform:** linux-ubuntu20.04-icelake
* **Concretizer:** clingo


### General information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have searched the issues of this repo and believe this is not a duplicate
- [X] I have run the failing commands in debug mode and reported the output
Revert "ASP-based solver: tweak heuristic, modify compiler encoding (#35989)"
This reverts commit 8517a74f3792bb87f754c6fd363d820104643d9e.

This breaks compiler version ranges



Discussion:
Should be solved by:
```diff
diff --git a/lib/spack/spack/solver/concretize.lp b/lib/spack/spack/solver/concretize.lp
index ca6d4baf13..df740445b4 100644
--- a/lib/spack/spack/solver/concretize.lp
+++ b/lib/spack/spack/solver/concretize.lp
@@ -915,6 +915,12 @@ error(2, "No valid version for '{0}' compiler '{1}' satisfies '@{2}'", Package,
      attr("node_compiler_version_satisfies", Package, Compiler, Constraint),
                 not compiler_version_satisfies(Compiler, Constraint, _).
 
+error(2, "No valid version for '{0}' compiler '{1}' satisfies '@{2}'", Package, Compiler, Constraint)
+  :- attr("node", Package),
+     attr("node_compiler_version_satisfies", Package, Compiler, Constraint),
+     not compiler_version_satisfies(Compiler, Constraint, ID),
+     node_compiler(Package, ID).
+
 % If the node is associated with a compiler and the compiler satisfy a constraint, then
 % the compiler associated with the node satisfy the same constraint
 attr("node_compiler_version_satisfies", Package, Compiler, Constraint)
```


Resolving patch:
diff --git a/lib/spack/spack/solver/asp.py b/lib/spack/spack/solver/asp.py
--- a/lib/spack/spack/solver/asp.py
+++ b/lib/spack/spack/solver/asp.py
@@ -1773,10 +1773,8 @@ def target_defaults(self, specs):
             # real_version from the compiler object to get more accurate
             # results.
             if not supported:
-                compiler_obj = spack.compilers.compilers_for_spec(compiler)
-                compiler_obj = compiler_obj[0]
                 supported = self._supported_targets(
-                    compiler.name, compiler_obj.real_version, candidate_targets
+                    compiler.name, compiler.real_version, candidate_targets
                 )
 
             if not supported:


Test patch:
diff --git a/lib/spack/spack/test/concretize.py b/lib/spack/spack/test/concretize.py
--- a/lib/spack/spack/test/concretize.py
+++ b/lib/spack/spack/test/concretize.py
@@ -2097,3 +2097,64 @@ def test_result_specs_is_not_empty(self, specs):
 
         assert result.specs
         assert not result.unsolved_specs
+
+    @pytest.mark.regression("36339")
+    def test_compiler_match_constraints_when_selected(self):
+        """Test that, when multiple compilers with the same name are in the configuration
+        we ensure that the selected one matches all the required constraints.
+        """
+        compiler_configuration = [
+            {
+                "compiler": {
+                    "spec": "gcc@11.1.0",
+                    "paths": {
+                        "cc": "/usr/bin/gcc",
+                        "cxx": "/usr/bin/g++",
+                        "f77": "/usr/bin/gfortran",
+                        "fc": "/usr/bin/gfortran",
+                    },
+                    "operating_system": "debian6",
+                    "target": "x86_64",
+                    "modules": [],
+                }
+            },
+            {
+                "compiler": {
+                    "spec": "gcc@12.1.0",
+                    "paths": {
+                        "cc": "/usr/bin/gcc",
+                        "cxx": "/usr/bin/g++",
+                        "f77": "/usr/bin/gfortran",
+                        "fc": "/usr/bin/gfortran",
+                    },
+                    "operating_system": "debian6",
+                    "target": "x86_64",
+                    "modules": [],
+                }
+            },
+        ]
+        spack.config.set("compilers", compiler_configuration)
+        s = spack.spec.Spec("a %gcc@:11").concretized()
+        assert s.compiler.version == ver("11.1.0"), s
+
+    @pytest.mark.regression("36339")
+    @pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows")
+    def test_compiler_with_custom_non_numeric_version(self, mock_executable):
+        """Test that, when a compiler has a completely made up version, we can use its
+        'real version' to detect targets and don't raise during concretization.
+        """
+        gcc_path = mock_executable("gcc", output="echo 9")
+        compiler_configuration = [
+            {
+                "compiler": {
+                    "spec": "gcc@foo",
+                    "paths": {"cc": gcc_path, "cxx": gcc_path, "f77": None, "fc": None},
+                    "operating_system": "debian6",
+                    "target": "x86_64",
+                    "modules": [],
+                }
+            }
+        ]
+        spack.config.set("compilers", compiler_configuration)
+        s = spack.spec.Spec("a %gcc@foo").concretized()
+        assert s.compiler.version == ver("foo")