@@ -145,84 +145,32 @@ void ConstraintSystem::mapImplicitConversions(Solution *solution)
145145 }
146146}
147147
148- bool ConstraintSystem::solveLocalConstraints (SolutionResult &result)
148+ bool ConstraintSystem::solveLocalConstraints (
149+ SolutionResult &result, SystemState const &initialState
150+ )
149151{
150- // / The initial system state used to begin constraint solving.
151- std::vector<SystemState> worklist;
152- worklist.emplace_back (_context ); // Start from an empty state
152+ // / The initial system state with early unification bindings applied
153+ std::vector<std::pair< SystemState, size_t > > worklist;
154+ worklist.push_back ({ initialState, 0 } ); // Start from the simplified state
153155
154156 while (!worklist.empty ()) {
155- SystemState current = std::move (worklist.back ());
157+ SystemState current = std::move (worklist.back ().first );
158+ size_t index = worklist.back ().second ;
156159 worklist.pop_back ();
157160
158- // / Apply non-defaultable constraints first
159- for (Constraint *constraint : _constraints) {
160- // Skip disabled constraints
161- if (constraint->isDisabled ())
162- continue ;
163-
164- // Skip defaultable constraints in first pass
165- if (constraint->getKind () == ConstraintKind::Defaultable
166- || constraint->isTypePropertyConstraint ()
167- || constraint->getKind () == ConstraintKind::StructInitialiser) {
168- continue ;
169- }
170-
171- // / Apply the constraint and check the result.
172- ConstraintResult result = apply (constraint, current, worklist);
173- markConstraint (result, constraint);
174- if (result == ConstraintResult::Failed) {
175- goto failed;
176- }
177- // Continue if Satisfied or Applied
178- }
179-
180- for (Constraint *constraint : _constraints) {
181- if (constraint->isDisabled ())
182- continue ;
183- if (constraint->getKind () != ConstraintKind::StructInitialiser)
184- continue ;
185- // / Apply the constraint and check the result.
186- ConstraintResult result = apply (constraint, current, worklist);
187- markConstraint (result, constraint);
188- if (result == ConstraintResult::Failed) {
189- goto failed;
190- }
191- }
161+ while (index < _constraints.size ()) {
162+ Constraint *constraint = _constraints[index++];
192163
193- // / Apply defaultable constraints only if non-defaultable
194- // / constraints succeed
195- for (Constraint *constraint : _constraints) {
196164 // Skip disabled constraints
197165 if (constraint->isDisabled ())
198166 continue ;
199167
200- // Only process defaultable constraints in second pass
201- if (constraint->getKind () != ConstraintKind::Defaultable)
202- continue ;
203-
204168 // / Apply the constraint and check the result.
205- ConstraintResult result = apply (constraint, current, worklist) ;
206- markConstraint ( result, constraint);
207- if (result == ConstraintResult::Failed ) {
208- goto failed ;
169+ std::vector<SystemState> newStates ;
170+ ConstraintResult result = apply ( constraint, current, newStates );
171+ for ( auto &newState : newStates ) {
172+ worklist. push_back ({ std::move (newState), index }) ;
209173 }
210- // Continue if Satisfied or Applied
211- }
212-
213- // / Apply ExpressibleByLiterals constraints only if other
214- // / constraints succeed
215- for (Constraint *constraint : _constraints) {
216- // Skip disabled constraints
217- if (constraint->isDisabled ())
218- continue ;
219-
220- // Only process ExpressibleByLiterals constraints in third pass
221- if (!constraint->isTypePropertyConstraint ())
222- continue ;
223-
224- // / Apply the constraint and check the result.
225- ConstraintResult result = apply (constraint, current, worklist);
226174 markConstraint (result, constraint);
227175 if (result == ConstraintResult::Failed) {
228176 goto failed;
@@ -253,6 +201,10 @@ bool ConstraintSystem::solveLocalConstraints(SolutionResult &result)
253201
254202bool ConstraintSystem::solveConstraints ()
255203{
204+ // Simplify constraints before solving and get initial state with early
205+ // bindings
206+ SystemState initialState = simplifyConstraints ();
207+
256208 // color the constraints based on which type variables they contain
257209 // map of color => set of used type variables
258210 std::vector<llvm::DenseSet<glu::types::TypeVariableTy *>> colors;
@@ -294,14 +246,16 @@ bool ConstraintSystem::solveConstraints()
294246 }
295247 } while (changed);
296248 // solve each color separately
297- SystemState finalSolution (_context);
249+ // Start with the initial state from simplification
250+ SystemState finalSolution = initialState;
251+
298252 for (std::size_t i = 0 ; i < colors.size (); ++i) {
299253 // Disable all constraints not in this color
300254 for (auto *constraint : _constraints) {
301255 constraint->setEnabled (colorConstraints[i].count (constraint));
302256 }
303257 SolutionResult result;
304- if (!solveLocalConstraints (result)) {
258+ if (!solveLocalConstraints (result, initialState )) {
305259 return false ;
306260 }
307261 result.getBestSolution ()->mergeInto (finalSolution);
0 commit comments