Running your code with the latest pytorch raises:
x-:tensor([0.6000], grad_fn=<AddBackward0>)
x-.grad: None
Traceback (most recent call last):
  File "foo.py", line 28, in <module>
    loss.backward(torch.ones_like(loss), retain_graph=True)
  File "torch/tensor.py", line 195, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "torch/autograd/__init__.py", line 99, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1]] is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

The problem is that the way the x.grad is updated during the forward in old version was done with the unsafe .data. And so the inplace operation was not properly detected.
This is a good example where the use of .data is dangerous and should be replaced by .detach() or with torch.no_grad().
You can fix your current code by doing this in old versions:
def optimizer(grad):
    return -w*grad.clone()