diff --git a/Lib/fontTools/feaLib/builder.py b/Lib/fontTools/feaLib/builder.py
index a378b3794..6758c08d5 100644
--- a/Lib/fontTools/feaLib/builder.py
+++ b/Lib/fontTools/feaLib/builder.py
@@ -1356,8 +1356,9 @@ class Builder(object):
"Empty glyph class in contextual substitution", location
)
# https://github.com/fonttools/fonttools/issues/512
+ # https://github.com/fonttools/fonttools/issues/2150
chain = self.get_lookup_(location, ChainContextSubstBuilder)
- sub = chain.find_chainable_single_subst(set(mapping.keys()))
+ sub = chain.find_chainable_single_subst(mapping)
if sub is None:
sub = self.get_chained_lookup_(location, SingleSubstBuilder)
sub.mapping.update(mapping)
diff --git a/Lib/fontTools/otlLib/builder.py b/Lib/fontTools/otlLib/builder.py
index b4ab36842..a72aa9fdb 100644
--- a/Lib/fontTools/otlLib/builder.py
+++ b/Lib/fontTools/otlLib/builder.py
@@ -764,7 +764,7 @@ class ChainContextSubstBuilder(ChainContextualBuilder):
result.setdefault(glyph, set()).update(replacements)
return result
- def find_chainable_single_subst(self, glyphs):
+ def find_chainable_single_subst(self, mapping):
"""Helper for add_single_subst_chained_()"""
res = None
for rule in self.rules[::-1]:
@@ -772,7 +772,7 @@ class ChainContextSubstBuilder(ChainContextualBuilder):
return res
for sub in rule.lookups:
if isinstance(sub, SingleSubstBuilder) and not any(
- g in glyphs for g in sub.mapping.keys()
+ g in mapping and mapping[g] != sub.mapping[g] for g in sub.mapping
):
res = sub
return res
diff --git a/Tests/feaLib/data/bug512.ttx b/Tests/feaLib/data/bug512.ttx
index 693ebeb7c..4ad3af2f9 100644
--- a/Tests/feaLib/data/bug512.ttx
+++ b/Tests/feaLib/data/bug512.ttx
@@ -73,7 +73,7 @@
-
+
@@ -94,7 +94,6 @@
-