diff --git a/mlir/lib/Parser/AttributeParser.cpp b/mlir/lib/Parser/AttributeParser.cpp --- a/mlir/lib/Parser/AttributeParser.cpp +++ b/mlir/lib/Parser/AttributeParser.cpp @@ -531,6 +531,13 @@ return nullptr; } + // Handle the case where no elements were parsed. + if (!hexStorage.hasValue() && storage.empty() && type.getNumElements()) { + p.emitError(loc) << "parsed zero elements, but type (" << type + << ") expected at least 1"; + return nullptr; + } + // Handle complex types in the specific element type cases below. bool isComplex = false; if (ComplexType complexTy = eltType.dyn_cast()) { diff --git a/mlir/test/IR/invalid.mlir b/mlir/test/IR/invalid.mlir --- a/mlir/test/IR/invalid.mlir +++ b/mlir/test/IR/invalid.mlir @@ -687,6 +687,11 @@ // ----- +// expected-error@+1 {{parsed zero elements, but type ('tensor') expected at least 1}} +#attr = dense<> : tensor + +// ----- + func @elementsattr_toolarge2() -> () { ^bb0: "foo"(){bar = dense<[-777]> : tensor<1xi8>} : () -> () // expected-error {{integer constant out of range}}