Skip to content

Commit 81bb19c

Browse files
malfetfacebook-github-bot
authored andcommittedSep 21, 2020
[JIT] Prohibit subscripted assignments for tuple types (pytorch#44929)
Summary: This would force jit.script to raise an error if someone tries to mutate tuple ``` Tuple[int, int] does not support subscripted assignment: File "/home/nshulga/test/tupleassignment.py", line 9 torch.jit.script def foo(x: Tuple[int, int]) -> int: x[-1] = x[0] + 1 ~~~~~ <--- HERE ``` Pull Request resolved: pytorch#44929 Reviewed By: suo Differential Revision: D23777668 Pulled By: malfet fbshipit-source-id: 8efaa4167354ffb4930ccb3e702736a3209151b6
1 parent 9a31eee commit 81bb19c

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed
 

‎aten/src/ATen/core/qualified_name.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct QualifiedName {
4545
atoms_ = atoms;
4646
cacheAccessors();
4747
}
48-
// Unnecessary copy. Ideally we'd use somoething like std::string_view.
48+
// Unnecessary copy. Ideally we'd use something like std::string_view.
4949
/* implicit */ QualifiedName(const char* name)
5050
: QualifiedName(std::string(name)) {}
5151

‎test/test_jit_py3.py

+11
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,17 @@ def annotated_fn(x: torch.Tensor) -> Tuple:
458458
with self.assertRaisesRegex(RuntimeError, r"Attempted to use Tuple without a contained type"):
459459
torch.jit.script(annotated_fn)
460460

461+
def test_tuple_subscripted_assign(self):
462+
with self.assertRaisesRegex(RuntimeError, "subscripted assignment"):
463+
@torch.jit.script
464+
def foo(a: Tuple[int, int]) -> None:
465+
a[0] = a[1]
466+
467+
with self.assertRaisesRegex(RuntimeError, "augmented assignment"):
468+
@torch.jit.script
469+
def bar(a: Tuple[int, int]) -> None:
470+
a[0] += a[1]
471+
461472
def test_subexpression_List_Future(self):
462473

463474
@torch.jit.script

‎torch/csrc/jit/frontend/ir_emitter.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,10 @@ struct to_ir {
22682268
<< " subscripted assignment. "
22692269
<< "File a bug if you want this";
22702270
}
2271+
if (sliceable->type()->isSubtypeOf(AnyTupleType::get())) {
2272+
throw ErrorReport(lhs) << sliceable->type()->repr_str()
2273+
<< " does not support subscripted assignment";
2274+
}
22712275

22722276
std::vector<NamedValue> args;
22732277
args.emplace_back(lhs.value().range(), "self", sliceable);

0 commit comments

Comments
 (0)
Please sign in to comment.