An appeals court ruling invalidating a rape conviction in California has sparked debate and was described by one legal expert as "bizarre."

A man who impersonates someone in order to have sexual intercourse may be guilty of rape only if the victim was married and the man was pretending to be her husband, a state appeals court has ruled.

The unanimous ruling, from an admittedly reluctant court, overturned the rape conviction of Julio Morales, who entered a sleeping woman's dark bedroom after her boyfriend walked out and began having intercourse with her. The woman screamed and resisted when she awoke and realized Morales was not her boyfriend, the court said.

"A man enters the dark bedroom of an unmarried woman after seeing her boyfriend leave late at night, and has sexual intercourse with the woman while pretending to be the boyfriend," the Los Angeles-based 2nd District Court of Appeals said in Wednesday's ruling. "Has the man committed rape? Because of historical anomalies in the law and the statutory definition of rape, the answer is no, even though, if the woman had been married and the man had impersonated her husband, the answer would be yes."

Santa Clara University law professor Gerald Uelmen called the ruling "bizarre" and likely to spark outrage, but predicted that the California Supreme Court would probably not review it because it was legally sound.

"I think the ball is in the Legislature's court," he said.

Uelmen said he found it "ironic" that a judge had spotted the anomaly in the law 30 years ago, yet the Legislature failed to change it. The ruling indicated there was "pretty solid" evidence the woman was sleeping during the sex, "so this guy isn't going to get off scot-free," the law professor said.

The court urged the Legislature to change the archaic law to "correct the incongruity that exists when a man may commit rape … when impersonating a husband, but not when impersonating a boyfriend."

The justices noted that prosecutors advanced two legal theories — that the defendant raped by tricking the victim, which applies only to married women, and that he committed rape by having sex with a sleeping person.