Index: src/ResolvExpr/Resolver.cc
===================================================================
--- src/ResolvExpr/Resolver.cc	(revision 1e8bbac9370b8c548860154f7bd8bb7610ca6ea8)
+++ src/ResolvExpr/Resolver.cc	(revision 882ad37047443824102ef071eaf49540b434c634)
@@ -74,4 +74,5 @@
 		void previsit( CatchStmt *catchStmt );
 		void previsit( WaitForStmt * stmt );
+		void previsit( WithStmt * withStmt );
 
 		void previsit( SingleInit *singleInit );
@@ -571,4 +572,46 @@
 	}
 
+	bool isStructOrUnion( Type * t ) {
+		t = t->stripReferences();
+		return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t );
+	}
+
+	void Resolver::previsit( WithStmt * withStmt ) {
+		for ( Expression *& expr : withStmt->exprs )  {
+			TypeEnvironment env;
+			AlternativeFinder finder( indexer, env );
+			finder.findWithAdjustment( expr );
+
+			// only struct- and union-typed expressions are viable candidates
+			AltList candidates;
+			for ( Alternative & alt : finder.get_alternatives() ) {
+				if ( isStructOrUnion( alt.expr->result ) ) {
+					candidates.push_back( std::move( alt ) );
+				}
+			}
+
+			// choose the lowest cost expression among the candidates
+			AltList winners;
+			findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) );
+			if ( winners.size() == 0 ) {
+				throw SemanticError( "No reasonable alternatives for with statement expression: ", expr );
+			} else if ( winners.size() != 1 ) {
+				std::ostringstream stream;
+				stream << "Cannot choose between " << winners.size() << " alternatives for with statement expression\n";
+				expr->print( stream );
+				stream << "Alternatives are:\n";
+				printAlts( winners, stream, 1 );
+				throw SemanticError( stream.str() );
+			}
+
+			// there is one unambiguous interpretation - move the expression into the with statement
+			Alternative & alt = winners.front();
+			finishExpr( alt.expr, alt.env, expr->env );
+			delete expr;
+			expr = alt.expr;
+			alt.expr = nullptr;
+		}
+	}
+
 	template< typename T >
 	bool isCharType( T t ) {
