The bill, which was passed by a cross-party majority in the Holyrood parliament last December, would have made Scotland the first country in the UK to introduce a self-identification system for people who want to change their legally recognised sex.
The Scottish government has 21 days to appeal against the ruling, but there are internal concerns that the SNP cannot afford further hefty legal fees at a time when voters want it to focus on the cost of living crisis.