From 47df88404ae2fd1e662660f4f355152645c0490a Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 7 Nov 2022 16:33:18 +0100 Subject: Simplify repeated _add_dependency calls for same package (#33732) --- lib/spack/spack/spec.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 30a67a55a8..ef4c3b7ab0 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -1558,26 +1558,24 @@ class Spec(object): def _add_dependency(self, spec, deptypes): """Called by the parser to add another spec as a dependency.""" - if spec.name in self._dependencies: - # allow redundant compatible dependency specifications - # depspec equality checks by name, so we need to check components - # separately to test whether the specs are identical - orig = self._dependencies[spec.name] - for dspec in orig: - if deptypes == dspec.deptypes: - try: - dspec.spec.constrain(spec) - return - except spack.error.UnsatisfiableSpecError: - raise DuplicateDependencyError( - "Cannot depend on incompatible specs '%s' and '%s'" - % (dspec.spec, spec) - ) - else: - raise DuplicateDependencyError("Cannot depend on '%s' twice" % spec) + if spec.name not in self._dependencies: + self.add_dependency_edge(spec, deptypes) + return - # create an edge and add to parent and child - self.add_dependency_edge(spec, deptypes) + # Keep the intersection of constraints when a dependency is added + # multiple times. Currently we only allow identical edge types. + orig = self._dependencies[spec.name] + try: + dspec = next(dspec for dspec in orig if deptypes == dspec.deptypes) + except StopIteration: + raise DuplicateDependencyError("Cannot depend on '%s' twice" % spec) + + try: + dspec.spec.constrain(spec) + except spack.error.UnsatisfiableSpecError: + raise DuplicateDependencyError( + "Cannot depend on incompatible specs '%s' and '%s'" % (dspec.spec, spec) + ) def add_dependency_edge(self, dependency_spec, deptype): """Add a dependency edge to this spec. -- cgit v1.2.3-60-g2f50