By James Mackintosh

Updated May 2, 2016 7:01 p.m. ET

Valeant, SunEdison…the S&P 500?

Financial engineering has built vast financial edifices, but all too often there is little of substance providing support. The latest Wall Street constructs to crumble are familiar names, Valeant Pharmaceuticals International Inc. and SunEdison Inc. Both relied on financial engineering to satisfy shareholders desperate for two items scarce in today’s weak economy: growth, offered by Valeant, and income, supplied by SunEdison’s so-called yieldcos.

Shareholders who avoided both will be congratulating themselves, or thanking their luck. But they should also be asking themselves a bigger question: Is the entire stock market engaged in unsustainable financial engineering in an effort to satisfy shareholders? Put another way: We know the market is engaged in large-scale financial engineering in the form of a huge ramp-up of leverage. Is it sustainable?

Valeant and SunEdison show financial engineering in a bad light. Valeant used access to cheap finance, in the form of its own highly valued stock and bonds, to make ever-bigger acquisitions. Aggressive accounting allowed it to goose its profits so long as the deals went on.

Because the company looked as though it was providing strong growth, investors marked up its shares, allowing it to do the next deal, and so on. Until it stopped.

Investors have learned once again the problem of “roll-up” companies where the growth is driven by accounting and valuation multiples rather than economies of scale. The virtuous circle of cheap financing for takeovers leading to higher valuations leading to cheap financing works only as long as they keep doing deals. Valeant’s market value is down $78 billion from its peak, about the same in today’s money as the total lost by shareholders of Enron.

SunEdison, a solar company, used more-novel financial engineering. It created twin yieldcos, TerraForm Power Inc. and TerraForm Global Inc., to raise cash from investors for its completed projects. By clever use of tax losses, shareholders were promised a high and rising dividend without corporate taxes, while SunEdison secured upfront cash and retained control of the projects.

Renewable-power companies created a line of yieldcos, gaining high valuations from income-starved investors, helped by those demanding environmentally friendly homes for their money. Almost all have been badly hurt by changes to state tax breaks for green projects and the cheapening of fossil fuels.

The financial engineering behind Valeant and SunEdison has a bad name, at least for now. But yesterday’s financial engineering often becomes widely accepted. In the case of the engineering behind today’s high stock-market valuations, it goes further: Borrowing to buy back shares is widely welcomed.

The biggest 1,500 nonfinancial companies in the U.S. increased their net debt by $409 billion in the year to the end of March, according to Société Générale, using almost all—$388 billion—to buy their own shares, net of newly issued stock. Companies have become far and away the biggest customer for their own shares.

As corporate profitability slows, the obvious worry is that this debt will lead shareholders to disaster. Total corporate debt is close to the proportion of the economy hit during the debt-fueled bubble that ended in the 2008 collapse of Lehman Brothers.

According to David Kostin, a strategist at Goldman Sachs, the debt and equity of the median U.S. nonfinancial company is worth 11 times operating cash flow, higher than in 2007 and higher than at the peak of the dot-com bubble. (This widely used measure is known as enterprise value to earnings before interest, taxes, depreciation and amortization).

So not only do companies have a lot of debt, but shareholders love it. What could possibly go wrong?

The case for calm is that when interest rates are low, it is only natural that companies will take on more debt. One illustration: The interest cover for U.S. junk-rated nonfinancials, or the number of times operating profit covers interest costs, is the strongest since at least 1997, according to the Office of Financial Research, a U.S. watchdog created by the Dodd-Frank Act.

The case for concern is that this borrowed money wasn’t invested into productive projects that would boost earnings in the future and so pay off the debt, but instead was used to buy back shares. Corporate investment has picked up recently, but overall buybacks are being funded from borrowing, as Andrew Lapthorne, a strategist at Société Générale, points out.

This creates three risks. First, rising interest rates for corporate debt would have a much bigger impact on profits and shareholders than usual, and could lead to a wave of defaults, as the energy sector showed.

Second, more caution in the bond market, perhaps caused by recession, could make it hard for companies to roll over debt; again, the energy sector has been a painful demonstration of what happens to highly indebted companies when they lose access to credit.

Third, if companies themselves become more cautious, it could remove the biggest buyer of stocks from the market. Share buybacks have helped support prices and earnings per share even as profits have fallen back to mid-2012 levels.

If companies stop borrowing to finance buybacks, another generation of shareholders will discover the downside of financial engineering.

Write to James Mackintosh at