Skip to content

Commit 077dbd4

Browse files
authored
Prevent creation of fields with one element (#2233)
1 parent a68dfd3 commit 077dbd4

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

src/Fraction.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ that it will always be returned by a call to the constructor when the same
821821
base ring $R$ is supplied.
822822
"""
823823
function fraction_field(R::Ring; cached::Bool=true)
824+
@req !is_trivial(R) "Base ring must not be the zero ring."
824825
return Generic.fraction_field(R; cached=cached)
825826
end
826827

src/ResidueField.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ residue ring parent object is cached and returned for any subsequent calls
466466
to the constructor with the same base ring $R$ and element $a$.
467467
"""
468468
function residue_field(R::Ring, a::RingElement; cached::Bool = true)
469+
@req !is_trivial(R) "Base ring must not be the zero ring."
469470
iszero(a) && throw(DivideError())
471+
@req !is_unit(a) "Cannot create a field with one element"
470472
T = elem_type(R)
471473
S = EuclideanRingResidueField{T}(R(a), cached)
472474
return S, Generic.EuclideanRingResidueMap(R, S)

test/generic/Fraction-test.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
@test !(a in [b])
5252
@test a in keys(Dict(a => 1))
5353
@test !(b in keys(Dict(a => 1)))
54+
55+
# trivial rings can not be fields
56+
R = residue_ring(ZZ, 1)[1]
57+
@test is_trivial(R)
58+
@test_throws ArgumentError fraction_field(R)
5459
end
5560

5661
@testset "Generic.FracFieldElem.printing" begin

test/generic/ResidueField-test.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767

6868
@test x in keys(Dict(x => 1))
6969
@test !(y in keys(Dict(x => 1)))
70+
71+
# trivial rings can not be fields
72+
@test_throws ArgumentError residue_field(ZZ, 1)
73+
R = residue_ring(ZZ, 1)[1]
74+
@test is_trivial(R)
75+
@test_throws ArgumentError residue_field(R, R(2))
7076
end
7177

7278
@testset "EuclideanRingResidueFieldElem.printing" begin

0 commit comments

Comments
 (0)