[BZ-1039586] fix PatternBuilder for patterns referencing this as a declaration
(cherry picked from commit 2682ab25109a04f2d56f91191354c135604981a0)